1. 程式人生 > >標準C++中的string類的用法總結(轉)

標準C++中的string類的用法總結(轉)

spa 大小 它的 world 包括 文本 語法 ner append()

轉自:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html

相信使用過MFC編程的朋友對CString這個類的印象應該非常深刻吧?的確,MFC中的CString類使用起來真的非常的方便好用。但是如果離開了MFC框架,還有沒有這樣使用起來非常方便的類呢?答案是肯定的。也許有人會說,即使不用MFC框架,也可以想辦法使用MFC中的API,具體的操作方法在本文最後給出操作方法。其實,可能很多人很可能會忽略掉標準C++中string類的使用。標準C++中提供的string類得功能也是非常強大的,一般都能滿足我們開發項目時使用。現將具體用法的一部分羅列如下,只起一個拋磚引玉的作用吧,好了,廢話少說,直接進入正題吧!

要想使用標準C++中string類,必須要包含

#include <string>// 註意是<string>,不是<string.h>,帶.h的是C語言中的頭文件

using std::string;

using std::wstring;

using namespace std;

下面你就可以使用string/wstring了,它們兩分別對應著char和wchar_t。

string和wstring的用法是一樣的,以下只用string作介紹:

構造函數(Constructors)

語法:

  string();
  string( size_type length, char ch );
  string( const char *str );
  string( const char *str, size_type length );
  string( string &str, size_type index, size_type length );
  string( input_iterator start, input_iterator end );

字符串的構造函數創建一個新字符串,包括:

  • 以length為長度的ch的拷貝(即length個ch)
  • 以str為初值 (長度任意),
  • 以index為索引開始的子串,長度為length, 或者
  • 以從start到end的元素為初值.

例如,

    string str1( 5, ‘c‘ );
    string str2( "Now is the time..." );
    string str3( str2, 11, 4 );
    cout << str1 << endl;
    cout << str2 << endl;
    cout << str3 << endl;

顯示

    ccccc
    Now is the time...
    time
    

操作符(Operators)

語法:

  ==
  >
  <
  >=
  <=
  !=
  +
  +=
  []

你可以用 ==, >, <, >=, <=, and !=比較字符串. 可以用 + 或者 += 操作符連接兩個字符串, 並且可以用[]獲取特定的字符.

相關主題:
at(), compare().


添加文本(append)

語法:

  basic_string &append( const basic_string &str );
  basic_string &append( const char *str );
  basic_string &append( const basic_string &str, size_type index, size_type len );
  basic_string &append( const char *str, size_type num );
  basic_string &append( size_type num, char ch );
  basic_string &append( input_iterator start, input_iterator end );

append() 函數可以完成以下工作:

  • 在字符串的末尾添加str,
  • 在字符串的末尾添加str的子串,子串以index索引開始,長度為len
  • 在字符串的末尾添加str中的num個字符,
  • 在字符串的末尾添加num個字符ch,
  • 在字符串的末尾添加以叠代器start和end表示的字符序列.

例如以下代碼:

    string str = "Hello World";
    str.append( 10, ‘!‘ );
    cout << str << endl;

顯示

    Hello World!!!!!!!!!!

相關主題:
+ 操作符


賦值(assign)

語法:

  basic_string &assign( const basic_string &str );
  basic_string &assign( const char *str );
  basic_string &assign( const char *str, size_type num );
  basic_string &assign( const basic_string &str, size_type index, size_type len );
  basic_string &assign( size_type num, char ch );

函數以下列方式賦值:

  • 用str為字符串賦值,
  • 用str的開始num個字符為字符串賦值,
  • 用str的子串為字符串賦值,子串以index索引開始,長度為len
  • 用num個字符ch為字符串賦值.

例如以下代碼:

    string str1, str2 = "War and Peace";
    str1.assign( str2, 4, 3 );  
    cout << str1 << endl;

顯示

    and
    

at

語法:

  reference at( size_type index );

at()函數返回一個引用,指向在index位置的字符. 如果index不在字符串範圍內, at() 將報告"out of range"錯誤,並拋出out_of_range異常。 比如下列代碼:

    string text = "ABCDEF";
    char ch = text.at( 2 );

顯示字符 ‘C‘.

相關主題:
[]操作符


begin

語法:

  iterator begin();

begin()函數返回一個叠代器,指向字符串的第一個元素.

相關主題:
end()


c_str

語法:

  const char *c_str();

c_str()函數返回一個指向正規C字符串的指針, 內容與本字符串相同.

相關主題:
[] 操作符


容量(capacity)

語法:

  size_type capacity();

capacity()函數返回在重新申請更多的空間前字符串可以容納的字符數. 這個數字至少與 size()一樣大.

相關主題:
max_size(), reserve(), resize(), size(),


比較(compare)

語法:

  int compare( const basic_string &str );
  int compare( const char *str );
  int compare( size_type index, size_type length, const basic_string &str );
  int compare( size_type index, size_type length, const basic_string &str, size_type index2,
  size_type length2 );
  int compare( size_type index, size_type length, const char *str, size_type length2 );

compare()函數以多種方式比較本字符串和str,返回:

返回值情況
小於零 this < str
this == str
大於零 this > str

不同的函數:

  • 比較自己和str,
  • 比較自己的子串和str,子串以index索引開始,長度為length
  • 比較自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
  • 比較自己的子串和str的子串,其中str的子串以索引0開始,長度為length2,自己的子串以index開始,長度為length

相關主題:
操作符


拷貝(copy)

語法:

  size_type copy( char *str, size_type num, size_type index );

copy()函數拷貝自己的num個字符到str中(從索引index開始)。返回值是拷貝的字符數


data

語法:

  const char *data();

data()函數返回指向自己的第一個字符的指針.

相關主題:
c_str()


empty

語法:

  bool empty();

如果字符串為空則empty()返回真(true),否則返回假(false).


end

語法:

  iterator end();

end()函數返回一個叠代器,指向字符串的末尾(最後一個字符的下一個位置).

相關主題:
begin()


刪除(erase)

語法:

  iterator erase( iterator pos );
  iterator erase( iterator start, iterator end );
  basic_string &erase( size_type index = 0, size_type num = npos );

erase()函數可以:

  • 刪除pos指向的字符, 返回指向下一個字符的叠代器,
  • 刪除從start到end的所有字符, 返回一個叠代器,指向被刪除的最後一個字符的下一個位置
  • 刪除從index索引開始的num個字符, 返回*this.

參數index num 有默認值, 這意味著erase()可以這樣調用:只帶有index以刪除index後的所有字符,或者不帶有任何參數以刪除所有字符. 例如:

    string s("So, you like donuts, eh? Well, have all the donuts in the world!");
    cout << "The original string is ‘" << s << "‘" << endl;
  
    s.erase( 50, 14 );
    cout << "Now the string is ‘" << s << "‘" << endl;

    s.erase( 24 );
    cout << "Now the string is ‘" << s << "‘" << endl;

    s.erase();
    cout << "Now the string is ‘" << s << "‘" << endl;

將顯示

    The original string is ‘So, you like donuts, eh? Well, have all the donuts in the world!‘
    Now the string is ‘So, you like donuts, eh? Well, have all the donuts‘
    Now the string is ‘So, you like donuts, eh?‘
    Now the string is ‘‘

查找(find)

語法:

  size_type find( const basic_string &str, size_type index );
  size_type find( const char *str, size_type index );
  size_type find( const char *str, size_type index, size_type length );
  size_type find( char ch, size_type index );

find()函數:

  • 返回str在字符串中第一次出現的位置(從index開始查找)如果沒找到則返回string::npos,
  • 返回str在字符串中第一次出現的位置(從index開始查找,長度為length)。如果沒找到就返回string::npos,
  • 返回字符ch在字符串中第一次出現的位置(從index開始查找)。如果沒找到就返回string::npos

例如,

    string str1( "Alpha Beta Gamma Delta" );
    unsigned int loc = str1.find( "Omega", 0 );
    if( loc != string::npos )
      cout << "Found Omega at " << loc << endl;
    else
      cout << "Didn‘t find Omega" << endl;
    
    

find_first_of

語法:

  size_type find_first_of( const basic_string &str, size_type index = 0 );
  size_type find_first_of( const char *str, size_type index = 0 );
  size_type find_first_of( const char *str, size_type index, size_type num );
  size_type find_first_of( char ch, size_type index = 0 );

find_first_of()函數:

  • 查找在字符串中第一個與str中的某個字符匹配的字符,返回它的位置。搜索從index開始,如果沒找到就返回string::npos
  • 查找在字符串中第一個與str中的某個字符匹配的字符,返回它的位置。搜索從index開始,最多搜索num個字符。如果沒找到就返回string::npos,
  • 查找在字符串中第一個與ch匹配的字符,返回它的位置。搜索從index開始。

相關主題:
find()


find_first_not_of

語法:

  size_type find_first_not_of( const basic_string &str, size_type index = 0 );
  size_type find_first_not_of( const char *str, size_type index = 0 );
  size_type find_first_not_of( const char *str, size_type index, size_type num );
  size_type find_first_not_of( char ch, size_type index = 0 );

find_first_not_of()函數:

  • 在字符串中查找第一個與str中的字符都不匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops
  • 在字符串中查找第一個與str中的字符都不匹配的字符,返回它的位置。搜索從index開始,最多查找num個字符。如果沒找到就返回string::nops
  • 在字符串中查找第一個與ch不匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops

相關主題:
find()


find_last_of

語法:

  size_type find_last_of( const basic_string &str, size_type index = npos );
  size_type find_last_of( const char *str, size_type index = npos );
  size_type find_last_of( const char *str, size_type index, size_type num );
  size_type find_last_of( char ch, size_type index = npos );

find_last_of()函數:

  • 在字符串中查找最後一個與str中的某個字符匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops
  • 在字符串中查找最後一個與str中的某個字符匹配的字符,返回它的位置。搜索從index開始,最多搜索num個字符。如果沒找到就返回string::nops
  • 在字符串中查找最後一個與ch匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops

相關主題:
find()


find_last_not_of

語法:

  size_type find_last_not_of( const basic_string &str, size_type index = npos );
  size_type find_last_not_of( const char *str, size_type index = npos);
  size_type find_last_not_of( const char *str, size_type index, size_type num );
  size_type find_last_not_of( char ch, size_type index = npos );

find_last_not_of()函數:

  • 在字符串中查找最後一個與str中的字符都不匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops
  • 在字符串中查找最後一個與str中的字符都不匹配的字符,返回它的位置。搜索從index開始,最多查找num個字符如果沒找到就返回string::nops
  • 在字符串中查找最後一個與ch不匹配的字符,返回它的位置。搜索從index開始。如果沒找到就返回string::nops

相關主題:
find()


get_allocator

語法:

  allocator_type get_allocator();

get_allocator()函數返回本字符串的配置器.


插入(insert)

語法:

  iterator insert( iterator i, const char &ch );
  basic_string &insert( size_type index, const basic_string &str );
  basic_string &insert( size_type index, const char *str );
  basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
  basic_string &insert( size_type index, const char *str, size_type num );
  basic_string &insert( size_type index, size_type num, char ch );
  void insert( iterator i, size_type num, const char &ch );
  void insert( iterator i, iterator start, iterator end );

insert()函數的功能非常多:

  • 在叠代器i表示的位置前面插入一個字符ch,
  • 在字符串的位置index插入字符串str,
  • 在字符串的位置index插入字符串str的子串(從index2開始,長num個字符),
  • 在字符串的位置index插入字符串str的num個字符,
  • 在字符串的位置index插入num個字符ch的拷貝,
  • 在叠代器i表示的位置前面插入num個字符ch的拷貝,
  • 在叠代器i表示的位置前面插入一段字符,從start開始,以end結束.

相關主題:
replace()


長度(length)

語法:

  size_type length();

length()函數返回字符串的長度. 這個數字應該和size()返回的數字相同.

相關主題:
size()


max_size

語法:

  size_type max_size();

max_size()函數返回字符串能保存的最大字符數。


rbegin

語法:

  const reverse_iterator rbegin();

rbegin()返回一個逆向叠代器,指向字符串的最後一個字符。

相關主題:
rend()


rend

語法:

  const reverse_iterator rend();

rend()函數返回一個逆向叠代器,指向字符串的開頭(第一個字符的前一個位置)。

相關主題:
rbegin()


替換(replace)

語法:

  basic_string &replace( size_type index, size_type num, const basic_string &str );
  basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
  size_type num2 );
  basic_string &replace( size_type index, size_type num, const char *str );
  basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
  basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
  basic_string &replace( iterator start, iterator end, const basic_string &str );
  basic_string &replace( iterator start, iterator end, const char *str );
  basic_string &replace( iterator start, iterator end, const char *str, size_type num );
  basic_string &replace( iterator start, iterator end, size_type num, char ch );

replace()函數:

  • 用str中的num個字符替換本字符串中的字符,從index開始
  • 用str中的num2個字符(從index2開始)替換本字符串中的字符,從index1開始,最多num1個字符
  • 用str中的num個字符(從index開始)替換本字符串中的字符
  • 用str中的num2個字符(從index2開始)替換本字符串中的字符,從index1開始,num1個字符
  • 用num2個ch字符替換本字符串中的字符,從index開始
  • 用str中的字符替換本字符串中的字符,叠代器start和end指示範圍
  • 用str中的num個字符替換本字符串中的內容,叠代器start和end指示範圍,
  • 用num個ch字符替換本字符串中的內容,叠代器start和end指示範圍.

例如,以下代碼顯示字符串"They say he carved it himself...find your soul-mate, Homer."

    string s = "They say he carved it himself...from a BIGGER spoon";
    string s2 = "find your soul-mate, Homer.";

    s.replace( 32, s2.length(), s2 );

    cout << s << endl;
  

相關主題:
insert()


保留空間(reserve)

語法:

  void reserve( size_type num );

reserve()函數設置本字符串的capacity 以保留num個字符空間。

相關主題:
capacity()


resize

語法:

  void resize( size_type num );
  void resize( size_type num, char ch );

resize()函數改變本字符串的大小到num, 新空間的內容不確定。也可以指定用ch填充。


rfind

語法:

  size_type rfind( const basic_string &str, size_type index );
  size_type rfind( const char *str, size_type index );
  size_type rfind( const char *str, size_type index, size_type num );
  size_type rfind( char ch, size_type index );

rfind()函數:

  • 返回最後一個與str中的某個字符匹配的字符,從index開始查找。如果沒找到就返回string::npos
  • 返回最後一個與str中的某個字符匹配的字符,從index開始查找,最多查找num個字符。如果沒找到就返回string::npos
  • 返回最後一個與ch匹配的字符,從index開始查找。如果沒找到就返回string::npos

例如,在下列代碼中第一次調用rfind()返回string::npos,因為目標詞語不在開始的8個字符中。然而,第二次調用返回9,因為目標詞語在開始的20個字符之中。

    int loc;
    string s = "My cat‘s breath smells like cat food.";

    loc = s.rfind( "breath", 8 );
    cout << "The word breath is at index " << loc << endl;

    loc = s.rfind( "breath", 20 );
    cout << "The word breath is at index " << loc << endl;
  

相關主題:
find()


size

語法:

  size_type size();

size()函數返回字符串中現在擁有的字符數。

相關主題:
length(), max_size()


substr

語法:

  basic_string substr( size_type index, size_type num = npos );

substr()返回本字符串的一個子串,從index開始,長num個字符。如果沒有指定,將是默認值 string::npos。這樣,substr()函數將簡單的返回從index開始的剩余的字符串。

例如:

    string s("What we have here is a failure to communicate");

    string sub = s.substr(21);

    cout << "The original string is " << s << endl;
    cout << "The substring is " << sub << endl;

顯示:

    The original string is What we have here is a failure to communicate
    The substring is a failure to communicate

交換(swap)

語法:

  void swap( basic_string &str );

swap()函數把str和本字符串交換。例如:

    string first( "This comes first" );
    string second( "And this is second" );
    first.swap( second );
    cout << first << endl;
    cout << second << endl;

顯示:

    And this is second
    This comes first

以上就是對C++ string類的一個簡要介紹。用的好的話它所具有的功能不會比MFC中的CString類遜色多少,呵呵,個人意見!

最後要介紹如何在Win32 應用程序中引用MFC中的部分類,例如CString。

1.在工程目錄下右鍵選擇"Properties”--->"Configuration Properties”--->“General”--->"Use of MFC"--->"Use MFC in a Static Library",

默認的是:"Use Standard Windows Libraries",如下圖:

      技術分享圖片

2.在你所用的所有頭文件之前包含#include <afxwin.h>,例如:可以在stdafx.h文件的最前面包含#include <afxwin.h>頭文件,這樣在你的源代碼中就可以使用

 CString類了,不過這樣也有一個缺點,就是編譯出來的程序要比原來的大很多。我試過一個小程序,選擇"Use Standard Windows Libraries" 編譯出來

 的Release版本大概92kb,使用"Use MFC in a Static Library"編譯出來的Release版本大概192kb,足足大了100kb,這個就個人考慮了......

標準C++中的string類的用法總結(轉)