string utf2oem( string const & in_str ) {
int n = MultiByteToWideChar( CP_UTF8, 0, in_str.data(), in_str.size(), NULL, 0 );
if( n == 0 )
return in_str;
wstring tmp;
tmp.resize( n );
int ret = MultiByteToWideChar(CP_UTF8, 0, in_str.data(), in_str.size(), &tmp.front(), tmp.size() );
if( ret == 0 )
return in_str;
string out_str;
out_str.resize( n );
ret = WideCharToMultiByte(CP_OEMCP, 0, tmp.data(), n, &out_str.front(), n, NULL, NULL);
return( ret == 0 ? in_str : out_str );
}
我尝试使用此功能但出现错误:error C2039: 'front' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
那么我可以在带有 boost 1.38 的 Visual C++ 2008 中使用一些东西代替 front() 吗?
最佳答案
Front 返回第一个元素,所以你可以 manually refer to it .您可以访问表中的元素。
&our_str[0] //insted of &our_str.front()
或者使用函数.data()专门为此制作的。但请记住“修改通过数据访问的字符数组是未定义的行为”。 (来自 en.cppreference.com)
our_str.data()
但是如果你需要一个迭代器开始,你可以使用.begin() .
our_str.begin()
有关字符串的更多信息,您可以阅读 here .
关于c++ - c++ 03 中是否有 front() 的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26350216/