c++ - c++ 03 中是否有 front() 的替代方案?

标签 c++ visual-studio-2008

   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/

相关文章:

c++ - OpenGL 着色器不绘制东西 :/

c++ - C++ 流中的eof()、fail()、bad() 和good() 之间有什么区别?

c++ - 变量的静态初始化失败

c++ - 无法将参数 1 从 'char' 转换为 'const std::basic_string<_Elem,_Traits,_Ax> &'

.net - Visual Studio Team System 2008 Developer Edition 上的代码覆盖率(在 NUnit 应用程序上)

c++ - 使用 cvSaveImage 错误的 OpenCV C++ 错误:在 cv::_InputArray::type 中断言失败((标志和 FIXED_TYPE)!= 0)

visual-studio-2008 - Web 应用程序项目类型在 Visual Studio 2008 中不可用

c++ - "Unexpected precompiled header error"是什么意思?

c++ - 如何在 VS 2008 中编译 64 位 native C++ DLL?

c++ - 根据 C++17 标准,为什么反向迭代器不是正式的迭代器类别?