簡體   English   中英

C ++ 03中的front()有替代方法嗎?

[英]Is there alternative for front() in c++ 03?

   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返回第一個元素,因此您可以手動引用它 您可以訪問表中的元素。

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

或使用專用於此的.data()函數。 但是請記住“修改通過數據訪問的字符數組是未定義的行為。” (來自en.cppreference.com)

our_str.data()

但是,如果需要開始使用迭代器,則可以使用.begin()

our_str.begin()

有關字符串的更多信息,您可以在這里閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM