c ++ 03中有front()的替代方案吗?

时间:2014-10-13 22:57:40

标签: 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>'

所以我可以在Visual C ++ 2008中使用boost(1.38?

)代替front()

1 个答案:

答案 0 :(得分:2)

Front返回第一个元素,因此您可以manually refer to it。您可以像访问表一样访问元素。

&our_str[0] //insted of &our_str.front()

或使用专门针对此功能.data()。但请记住&#34;修改通过数据访问的字符数组是未定义的行为。&#34; (来自en.cppreference.com)

our_str.data()

但是如果你需要一个迭代器来开始,你可以使用.begin()

our_str.begin()

有关字符串的更多信息,请阅读here