C++ string類型詳解
string是非常強大的類型,很好的封裝了字串的操作,有些時候我們可以把string當做字元的容器,string也
支援大多數容器操作,下面就列出string類型所支援的所有操作,本文並不是為了講解string的用法和應用,
而是希望作為string類型的參考文檔,每個函數皆在注釋後有詳細說明,需要用時查閱即可。
1.建構函式
string();//空串string(size_type length,char ch);//以length為長度的ch的拷貝(即length個ch)string(const char *str);//以str為初值 (長度任意)string(const char *str,size_type length);//同上,長度不限,但注意不要越界,以免發生不可預知問題string(string &str, size_type index, size_type length);//以index為索引開始的子串,長度為length, 或者小於lengthstring(input_iterator begin, input_iterator end);//以從start到end的元素為初值
2.支援的操作符
==
>
<
>=
<=
!=
+
+=
[ ]
3.追加文本(append)
basic_string &append(const basic_string &str);//在字串的末尾添加strbasic_string &append(const char *str);//在字串末尾添加str所指向的c風格字串basic_string &append(const basic_string &str,size_type index,size_type len);//在字串的末尾添加str的子串,子串以index索引開始,長度為lenbasic_string &append(const char *str,size_type num);//在字串的末尾添加str中的num個字元basic_string &append(size_type num,char ch);//在字串的末尾添加num個字元chbasic_string &append(input_iterator start,input_iterator end);//在字串的末尾添加以迭代器start和end表示的字元序列push_back('k');//把一個字元串連到當前字串的結尾
4.賦值(assign)
basic_string &assign(const basic_string &str);//用str為字串賦值basic_string &assign(const char *str);//用str c風格為字串賦值basic_string &assign(const char *str,size_type num);//用str的開始num個字元為字串賦值basic_string &assign(const basic_string &str,size_type index,size_type len);//用str的子串為字串賦值,子串以index索引開始,長度為lenbasic_string &assign(size_type num,char ch);//用num個字元ch為字串賦值string &assign(const_iterator begin,const_itertor end);//把first和last迭代器之間的部分賦給字串
5.比較(compare)
int compare(const basic_string &str);//比較自己和strint compare(size_type index,size_type length,const basic_string &str);//比較自己的子串和str,子串以index索引開始,長度為lengthint compare(size_type index,size_type length,const basic_string &str,size_type index2,size_type length2);//比較自己的子串和str的子串,其中index2和length2引用str,index和length引用自己int compare(const char *str);//比較自己和strint compare(int pos, int n,const char *s)//比較自己的子串,從pos開始,n個字元,和s進行比較int compare(size_type index,size_type length,const char *str,size_type length2);//比較自己的子串和str的子串,其中str的子串以索引0開始,長度為length2,自己的子串//以index開始,長度為length
傳回值 情況
小於零 this < str
零 this == str
大於零 this > str
6.刪除(erase)
iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字元,返回刪除後迭代器的位置iterator erase(iterator it);//刪除it指向的字元,返回刪除後迭代器的位置string &erase(int pos = 0, int n = npos);//刪除pos開始的n個字元,返回修改後的字串
7.插入(insert)
iterator insert(iterator i,const char &ch);//在迭代器i表示的位置前面插入一個字元chbasic_string &insert(size_type index,const basic_string &str);//在字串的位置index插入字串strbasic_string &insert(size_type index,const char *str);//在字串的位置index插入字串strbasic_string &insert(size_type index1,const basic_string &str,size_type index2,size_type num);//在字串的位置index插入字串str的子串(從index2開始,長num個字元)basic_string &insert(size_type index,const char *str,size_type num);//在字串的位置index插入字串str的num個字元basic_string &insert(size_type index,size_type num,char ch );//在字串的位置index插入num個字元ch的拷貝void insert(iterator i,size_type num,const char &ch);//在迭代器i表示的位置前面插入num個字元ch的拷貝void insert(iterator i,iterator begin,iterator end );//在迭代器i表示的位置前面插入一段字元,從start開始,以end結束
8.替換(replace)
basic_string &replace(size_type index,size_type num,const basic_string &str);//用str中的num個字元替換本字串中的字元,從index開始replace(size_type index1,size_type num1,const basic_string &str,size_type index2,size_type num2);//用str中的num2個字元(從index2開始)替換本字串中的字元,從index1開始,最多num1個字元basic_string &replace(size_type index,size_type num,const char *str);//用str中的num個字元(從index開始)替換本字串中的字元basic_string &replace(size_type index,size_type num1,const char *str,size_type num2);//用str中的num2個字元(從index2開始)替換本字串中的字元,從index1開始,num1個字元basic_string &replace(size_type index,size_type num1,size_type num2,char ch);//用num2個ch字元替換本字串中的字元,從index開始,num1個字元basic_string &replace(iterator start,iterator end,const basic_string &str);//用str中的字元替換本字串中的字元,迭代器start和end指示範圍basic_string &replace(iterator start,iterator end,const char *str);//用str替換本字串中的內容,迭代器start和end指示範圍basic_string &replace(iterator start,iterator end,const char *str,size_type num );//用str中的num個字元替換本字串中的內容,迭代器start和end指示範圍basic_string &replace(iterator start,iterator end,size_type num,char ch );//用num個ch字元替換本字串中的內容,迭代器start和end指示範圍
9.尋找
函數 find:size_type find( const basic_string &str, size_type index );//返回str在字串中第一次出現的位置(從index開始尋找)size_type find( const char *str, size_type index );//返回str在字串中第一次出現的位置(從index開始尋找)size_type find( const char *str, size_type index, size_type length );//返回str在字串中第一次出現的位置(從index開始尋找,長度為length)size_type find( char ch, size_type index );//返回字元ch在字串中第一次出現的位置(從index開始尋找)函數 find_first_of:尋找在字串中第一個與str中的某個字元匹配的字元 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_not_of:在字串中尋找第一個與str中的字元都不匹配的字元 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_last_of:在字串中尋找最後一個與str中的某個字元匹配的字元 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_not_of:在字串中尋找最後一個與str中的字元都不匹配的字元 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 );rfind函數 size_type rfind( const basic_string &str, size_type index ); //返回最後一個與str中的某個字元匹配的字元,從index開始尋找 size_type rfind( const char *str, size_type index ); //返回最後一個與str中的某個字元匹配的字元,從index開始尋找 size_type rfind( const char *str, size_type index, size_type num ); //返回最後一個與str中的某個字元匹配的字元,從index開始尋找,最多尋找num個字元 size_type rfind( char ch, size_type index ); //返回最後一個與ch匹配的字元,從index開始尋找
10.其他函數
at函數 reference at( size_type index ); //at()函數返回一個引用,指向在index位置的字元. 如果index //不在字串範圍內, at() 將報告"out of range"錯誤,並拋出out_of_range異常begin函數 iterator begin();//begin()函數返回一個迭代器,指向字串的第一個元素end函數 iterator end();//返回一個迭代器,指向字串的末尾(最後一個字元的下一個位置)c_str函數 const char *c_str();//返回一個指向正規C字串的指標, 內容與本字串相同capacity函數 size_type capacity();//返回在重新申請更多的空間前字串可以 //容納的字元數. 這個數字至少與 size()一樣大copy函數 size_type copy( char *str, size_type num, size_type index ); //拷貝自己的num個字元到str中(從索引index開始)。傳回值是拷貝的字元數data函數 const char *data();//返回指向自己的第一個字元的指標empty函數 bool empty();//如果字串為空白則empty()返回真(true),否則返回假(false)get_allocator函數 allocator_type get_allocator();//返回本字串的配置器length函數 size_type length();//返回字串的長度. 這個數字應該和size()返回的數字相同max_size size_type max_size();//返回字串能儲存的最大字元數rbegin函數 rbegin();//返回一個逆向迭代器,指向最後一個字元rend函數 rend();//返回一個逆向迭代器,指向第一個元素的前一個位置reserve函數 reserve( size_type num );//保留一定容量以容納字串(設定capacity值)resize函數 void resize( size_type num );//改變本字串的大小到num, 新空間的內容不確定 void resize( size_type num, char ch );//也可以指定用ch填充size函數 size();//返回字串中字元的數量substr函數 basic_string substr( size_type index, size_type num = npos ); //返回本字串的一個子串,從index開始,長num個字元。如果沒有指定, //將是預設值 string::npos。這樣,substr()函數將簡單的返回從index開始的剩餘的字串swap函數 void swap( basic_string &str );//把str和本字串交換
11.樣本
#include <iostream>#include <string>#include <sstream>using namespace std;int main(){ //1.string類重載運算子operator>>用於輸入,同樣重載運算子operator<<用於輸出操作 string str1; cin >> str1;//當用cin>>進行字串的輸入的時候,遇到空格的地方就停止字串的讀取輸入 cout << str1 << endl; cin.get();//這個的作用就是讀取cin>>輸入的結束符,不用對getline的輸入產生影響。 getline(cin, str1);//字串的行輸入 cout << str1 << endl; //2.string類的建構函式 string str2 = "aaaaa";//最簡單的字串初始化 cout << str2 << endl; char *s = "bbbbb"; string str3(s);//用c字串s初始化 cout << str3 << endl; char ch = 'c'; string str4(5, ch);//用n個字元ch初始化 cout << str4 << endl; //3.string類的字元操作 string str5 = "abcde"; ch = str5[3];//operator[]返回當前字串中第n個字元的位置 cout << ch << endl; string str6 = "abcde"; ch = str6.at(4);//at()返回當前字串中第n個字元的位置,並且提供範圍檢查,當越界時會拋出異常。 cout << ch << endl; //4.string的特性描述 string str7 = "abcdefgh"; int size; size = str7.capacity();//返回當前容量 cout << size << endl; size = str7.max_size();//返回string對象中可存放的最大字串的長度 cout << size << endl; size = str7.size();//返回當前字串的大小 cout << size << endl; size = str7.length();//返回當前字串的長度 cout << size << endl; bool flag; flag = str7.empty();//判斷當前字串是否為空白 cout << flag << endl; int len = 10; str7.resize(len, ch);//把字串當前大小置為len,並用字元ch填充不足的部分 cout << str7 << endl; //5.string的賦值 string str8; str8 = str7;//把字串str7賦給當前字串 cout << str8 << endl; str8.assign(str7);//把字串str7賦給當前字串 cout << str8 << endl; str8.assign(s);//用c類型字串s賦值 cout << str8 << endl; str8.assign(s, 2);//用c類型字串s開始的n個字元賦值 cout << str8 << endl; str8.assign(len, ch);//用len個字元ch賦值給當前字串 cout << str8 << endl; str8.assign(str7, 0, 3);//把字串str7中從0開始的3個字元賦給當前字串 cout << str8 << endl; string str9 = "0123456789"; str8.assign(str9.begin(), str9.end());//把迭代器之間的字元賦給字串 cout << str8 << endl; //6.string的串連 string str10; str10 += str9;//把字串str9串連到當前字串的結尾 cout << str10 << endl; str10.append(s);//把c類型字串s串連到當前字串的結尾 cout << str10 << endl; str10.append(s, 2);//把c類型字串s的前2個字元串連到當前字串的結尾 cout << str10 << endl; str10.append(str9.begin(), str9.end());//把迭代器之間的一段字元串連到當前字串的結尾 cout << str10 << endl; str10.push_back('k');//把一個字元串連到當前字串的結尾 cout << str10 << endl; //7.string的比較 flag = (str9 == str10);//判斷兩個字串是否相等 cout << flag << endl; flag = (str9 != str10);//判斷兩個字串是否不相等 cout << flag << endl; flag = (str9 > str10);//判斷兩個字串是否大於關係 cout << flag << endl; flag = (str9 < str10);//判斷兩個字串是否為小於關係 cout << flag << endl; flag = (str9 >= str10);//判斷兩個字串是否為大於等於關係 cout << flag << endl; flag = (str9 <= str10);//判斷兩個字串否為小於等於關係 cout << flag << endl; //以下的3個函數同樣適用於c類型的字串,在compare函數中>時返回1,<時返回-1,=時返回0 flag = str10.compare(str9);//比較兩個字串的大小,通過ASCII的相減得出。 cout << flag << endl; flag = str10.compare(6, 12, str9);//比較str10字串從6開始的12個字元組成的字串與str9的大小 cout << flag << endl; flag = str10.compare(6, 12, str9, 3, 5);//比較str10字串從6開始的12個字元組成的字串與str9字串從3開始的5個字元組成的字串的大小 cout << flag << endl; //8.string的字串 string str11; str11 = str10.substr(10, 15);//返回從下標10開始的15個字元組成的字串 cout << str11 << endl; //9.string的交換 str11.swap(str10);//交換str11與str10的值 cout << str11 << endl; //10.string的尋找,尋找成功時返回所在位置,失敗時返回string::npos的值,即是-1 string str12 = "abcdefghijklmnopqrstuvwxyz"; int pos; pos = str12.find('i', 0);//從位置0開始尋找字元i在當前字串的位置 cout << pos << endl; pos = str12.find("ghijk", 0);//從位置0開始尋找字串“ghijk”在當前字串的位置 cout << pos << endl; pos = str12.find("opqrstuvw", 0, 4);//從位置0開始尋找字串“opqrstuvw”前4個字元組成的字串在當前字串中的位置 cout << pos << endl; pos = str12.rfind('s', string::npos);//從字串str12反向開始尋找字元s在字串中的位置 cout << pos << endl; pos = str12.rfind("klmn", string::npos);//從字串str12反向開始尋找字串“klmn”在字串中的位置 cout << pos << endl; pos = str12.rfind("opqrstuvw", string::npos, 3);//從string::pos開始從後向前尋找字串s中前n個字元組成的字串在當前串中的位置 cout << pos << endl; string str13 = "aaaabbbbccccdddeeefffggghhhiiijjjkkllmmmandjfaklsdfpopdtwptioczx"; pos = str13.find_first_of('d', 0);//從位置0開始尋找字元d在當前字串第一次出現的位置 cout << pos << endl; pos = str13.find_first_of("eefff", 0);//從位置0開始尋找字串“eeefff“在當前字串中第一次出現的位置 cout << pos << endl; pos = str13.find_first_of("efff", 0, 3);//從位置0開始尋找當前串中第一個在字串”efff“的前3個字元組成的數組裡的字元的位置 cout << pos << endl; pos = str13.find_first_not_of('b', 0);//從當前串中尋找第一個不在串s中的字元出現的位置 cout << pos << endl; pos = str13.find_first_not_of("abcdefghij", 0);//從當前串中尋找第一個不在串s中的字元出現的位置 cout << pos << endl; pos = str13.find_first_not_of("abcdefghij", 0, 3);//從當前串中尋找第一個不在由字串”abcdefghij”的前3個字元所組成的字串中的字元出現的位置 cout << pos << endl; //下面的last的格式和first的一致,只是它從後面檢索。 pos = str13.find_last_of('b', string::npos); cout << pos << endl; pos = str13.find_last_of("abcdef", string::npos); cout << pos << endl; pos = str13.find_last_of("abcdef", string::npos, 2); cout << pos << endl; pos = str13.find_last_not_of('a', string::npos); cout << pos << endl; pos = str13.find_last_not_of("abcdef", string::npos); cout << pos << endl; pos = str13.find_last_not_of("abcdef", string::npos, 3); cout << pos << endl; //11.string的替換 string str14 = "abcdefghijklmn"; str14.replace(0, 3, "qqqq");//刪除從0開始的3個字元,然後在0處插入字串“qqqq” cout << str14 << endl; str14.replace(0, 3, "vvvv", 2);//刪除從0開始的3個字元,然後在0處插入字串“vvvv”的前2個字元 cout << str14 << endl; str14.replace(0, 3, "opqrstuvw", 2, 4);//刪除從0開始的3個字元,然後在0處插入字串“opqrstuvw”從位置2開始的4個字元 cout << str14 << endl; str14.replace(0, 3, 8, 'c');//刪除從0開始的3個字元,然後在0處插入8個字元 c cout << str14 << endl; //上面的位置可以換為迭代器的位置,操作是一樣的,在這裡就不再重複了。 //12.string的插入,下面的位置處亦可以用迭代器的指標表示,操作是一樣的 string str15 = "abcdefg"; str15.insert(0, "mnop");//在字串的0位置開始處,插入字串“mnop” cout << str15 << endl; str15.insert(0, 2, 'm');//在字串的0位置開始處,插入2個字元m cout << str15 << endl; str15.insert(0, "uvwxy", 3);//在字串的0位置開始處,插入字串“uvwxy”中的前3個字元 cout << str15 << endl; str15.insert(0, "uvwxy", 1, 2);//在字串的0位置開始處,插入從字串“uvwxy”的1位置開始的2個字元 cout << str15 << endl; //13.string的刪除 string str16 = "gfedcba"; string::iterator it; it = str16.begin(); it++; str16.erase(it);//刪除it指向的字元,返回刪除後迭代器的位置 cout << str16 << endl; str16.erase(it, it+3);//刪除it和it+3之間的所有字元,返回刪除後迭代器的位置 cout << str16 << endl; str16.erase(2);//刪除從字串位置3以後的所有字元,返回位置3前面的字元 cout << str16 << endl; //14.字串的流處理 string str17("hello,this is a test"); istringstream is(str17); string s1,s2,s3,s4; is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test" ostringstream os; os<<s1<<s2<<s3<<s4; cout<<os.str() << endl; //system("pause");}
參考部落格:http://blog.csdn.net/ro_wsy/article/details/7701218