STL學習筆記-- string


 
string 基本字符序列容器

    C語言並沒有提供一個專門的字符串類型,需要通過字符數組,對字符串進行存儲和處理。字符數組的末尾是一個值為 0 的 null 字符,表示字符串的結束。從而,一個用於存儲 n 個字符的字符數組,字符個數為 n+1 。基於這樣的字符數組,就可實現字符串的字符添加、刪除、搜索、替換、連接和子串操作等。
    在標准 C++ 中,字符串類 string 由 C++ STL 實現,提供豐富的字符串的處理功能。string 是一個基於字符的序列容器,具有vector向量容器一樣的內部線性結構,字符逐一寫入容器,最后以 null 字符結束。雖然 vector<char> 的成員函數也具有一定的字符串操作性質,但是不及string 接口豐富,易於使用和擴充容器。
    
    使用 string 基本字符序列容器,必須先通過宏語句 "#include <string>" 將 string 的實現代碼包含進來,才可編譯程序。
   

    
創建 string 對象
字符串的操作,需要先用 string 類型的構造函數,構造出一個 string 對象,為隨后添加的字符分配內存,並初始化 string 對象的內部變量。如下是它的幾個常用的構造函數原型。
1.    string()
    默認的構造函數,用於創建一個不包含任何非空字符的 string 對象。
    例如,下面一行代碼創建空的 string 對象 s .
    string  s;

2    string(const string& s,  size_type pos = 0, size_type n=npos)
    拷貝構造函數,將 string 對象 s 的 [pos, pos+n) 中的字符拷貝到新創建的 string 對象空間中,其中 string::npos 已定義為 -1。當 pos取值0,n取值 string::npos 時,就是用整個字符串 s 拷貝創建一個新的 string 對象。
    例如,下面代碼拷貝創建 string 對象 s2
    // string  s1;
    string  s2(s1);
    
3.    string(const char*)
    將 char* 數組的字符拷貝到新創建的 string 對象空間中。
    例如,下面代碼將字符數組 cArray 的3個字符,拷貝到 string 對象 s 中。
    char*  cArray="abc";
    string  s(cArray);   // s 包含字符 'a', 'b', 'c' 和一個 null 字符

 

 

 

字符添加
 1 /*    字符的添加
 2     string 對象具有如下幾個常用的添加字符或字符串的函數,並提供了與這些添加函數相對應的更方便的字符串 "+" 和 "+=" 操作。
 3     void  push_back(char c)    // 在 string 對象的字符串尾部添加一個【字符】 c
 4     string&  append(const char* s)    // 在 string 對象的字符串尾部添加【字符串】 s
 5     string&  append(const string& s)    // 在 string 對象的字符串尾部添加 s 對象的【字符串】
 6     iterator  insert(iterator pos, const char& c)     // 在 string 對象的 pos 位置之前,插入一個字符 c
 7     
 8     下面的示例程序創建 string 對象 str1 和 str2 ,然后使用上面的函數或操作,進行字符或字符串的添加操作。
 9 */
10 
11 -------------------------------------------------------- 字符添加
12 #include <string>
13 #include <iostream>
14 using namespace std;
15 int main()
16 {
17     string str1("abcd");
18     /*
19          VC6.0 版本的編譯器的 STL 有點點老舊了。不支持string 下面的 push_back 操作,
20     在vs2010下面測試,string 里面,是支持 push_back操作的
21     */
22     //    str1.push_back('a');
23     //    str1.push_back('b');
24     //    str1.push_back('c');
25     //    str1.push_back('d');
26     cout << "打印str1: "<< str1 << endl;
27     
28     char* cArray = "efgh";
29     string str2(cArray);
30     cout << "打印str2: " << str2 << endl;
31     
32     // 字符串的 + 操作
33     cout << "打印str1+str2: " << str1+str2 << endl;
34     
35     // 字符串 str1后添加字符串 str2
36     cout << "append后,打印 str1: " << str1.append(str2) << endl;
37     
38     // 在 str1 的第2個字符位置前插入字符 '8'
39     string::iterator  i;
40     i = str1.begin();
41     i++;
42     str1.insert(i, '8');
43     cout << "insert后,打印 str1: " << str1 << endl;
44     
45     // 字符串的 "+=" 操作
46     str1 += str2;
47     cout << "str1+=str2 后,打印 str1: " << str1 << endl;
48     
49     return 0;
50 }

 

 

 

字符的遍歷
 1 /*    字符的遍歷訪問
 2     string 字符串提供了數組操作符 “[]” ,用於訪問指定索引位置處的字符。一般的字符遍歷則是使用前向迭代器和反向迭代器來進行。此時,通常要用 begin/rbegin 和 end/rend 函數找出遍歷開始的首字符和末字符,然后通過迭代器的 “++” 和 “*” 操作,讀取字符串中的字符。
 3     
 4     下面的示例程序,先用數組方式,將字符串的字符逐個打印出來。然后,用反向迭代器,將字符串逆序打印:
 5 */
 6 
 7 -------------------------------------------------------- 字符的遍歷
 8 #include <string>
 9 #include <iostream>
10 using namespace std;
11 int main()
12 {
13     char*  cArray = "Hello, world!";
14     string str(cArray);
15     // 數組方式
16     for (size_t j=0; j<str.size(); j++)
17         cout << "" << j << "個字符:" << str[j] << endl;
18 
19     // 迭代器方式
20     string::reverse_iterator  ri, rend;
21     rend = str.rend();
22     cout << "反向打印字符:" << endl;
23     for (ri=str.rbegin(); ri!=rend; ++ri)
24         cout << *ri << "  ";
25     cout << endl;
26 
27     return 0;
28 }

 

 

 

字符的刪除
 1 /*    字符的刪除
 2     字符串中字符的刪除,可使用如下幾個重載的 erase 和 clear 函數,分別刪除指定位置上的若干字符或刪除所有的字符。
 3     iterator  erase(iterator pos)    // 刪除字符串中 position 所指的字符
 4     iterator  erase(iterator first, iterator last)    // 刪除字符串中迭代器區間 [first, last) 上的所有字符
 5     string& erase(size_type pos=0, size_type n=npos)    // 刪除字符串中從索引位置 pos 開始的 n個字符。
 6     void clear()    // 刪除字符串中的所有字符
 7     
 8     下面的程序給出了字符刪除的示例:
 9 */
10 -------------------------------------------------------- 字符的刪除
11 #include <string>
12 #include <iostream>
13 using namespace std;
14 int main()
15 {
16     char*  cArray = "a12345678b";
17     string str(cArray);
18     // 刪除第一個字符 
19     str.erase(str.begin());
20     // 打印出 12345678b
21     cout << str << endl;
22     // 刪除操作
23     str.erase(str.begin()+3, str.end()-2);
24     // 打印出 1238b
25     cout << str << endl;
26 
27     // str.clear();  // 在VC6.0 下面又沒有編譯通過,VC6.0的 STL 有些老舊了
28 
29     return 0;
30 }

 

 

 

字符的替換
 1 /*    字符的替換
 2     string 提供了相當多的字符替換的重載函數 replace ,可將指定索引位置或迭代器位置處的字符替換。如下是幾個常用的函數原型說明:
 3     string&  replace(size_type pos, size_type n, const char* s) // 將當前字符串從 pos 索引位置開始的 n 個字符,替換為字符串 s
 4     string&  replace(size_type pos, size_type n, size_type n1, char c)    // 將當前字符串從 pos 索引位置開始的 n 個字符,替換為 n1 個字符 c 
 5     string&  replace(iterator first, iterator last, const char* s)    // 將當前字符串的 [first, last) 區間中的字符替換為字符串 s
 6 */
 7 
 8 -------------------------------------------------------- 字符的替換
 9 #include <string>
10 #include <iostream>
11 using namespace std;
12 int main()
13 {
14     char*  cArray = "hello,boy!";
15     string str(cArray);
16     // boy 替換 girl
17     str.replace(6,3,"girl");
18     // 打印  hello girl!
19     cout << str << endl;
20 
21     // 將“!”替換為“。”
22     str.replace(10, 1, 1, '.');
23     // 打印 hello girl.
24     cout << str << endl;
25 
26     str.replace(str.begin(), str.begin()+5, "good morning");
27     // 打印 good morning girl.
28     cout << str << endl;
29 
30     return 0;
31 }

 

 

 

字符的搜索
 1 /*    字符的搜索
 2     string 提供了如下幾個比較常用的在字符串中搜索匹配的字符串或字符的函數
 3     
 4     size_type  find(const char* s, size_type pos=0)    // 在當前字符串 pos 索引位置開始,查找子串 s ,返回找到的位置索引, -1 表示查找不到子串。
 5     size_type  find(char c, size_type pos=0)    // 在當前字符串的 pos 索引位置開始,查找字符串 c 的索引位置,返回 -1 表示查找不到字符 c
 6 
 7     size_type  rfind(const char* s, size_type pos=npos)    // 從當前字符串的 pos 索引位置開始,反向順序查找子串 s ,返回找到字符串的索引位置, -1 表示查找不到該子串。
 8     size_type  rfind(char c, size_type pos=npos)    // 從當前字符串的 pos 索引位置開始,查找位於子串 s 的字符,返回找到字符的索引位置, -1 表示查找不到該字符。
 9 
10     size_type  find_first_of(const char* s, size_type pos=0)    // 從當前字符串的 pos 索引位置開始,查找位於子串 s 的字符,返回找到字符的索引位置, -1 表示查找不到。
11     size_type  find_first_not_of()    //從當前字符串的 pos 索引位置開始,查找第1個不位於子串 s 的字符,返回找到字符的索引位置, -1 表示查找不到。
12 
13     size_type  find_last_of()    // 從當前字符串的 pos 索引位置開始,查找最后一個子串 s 的字符,返回找到的字符的索引位置, -1 表示查找不到字符。
14     size_type  find_last_not_of()    // 從當前字符串的 pos 索引位置開始,查找最后一個不位於子串 s 的字符,返回找到的字符的索引位置, -1 表示查找不到字符。
15     
16     下面的示例程序,使用了以上的函數進行字符串和字符的搜索
17 */
18 
19 -------------------------------------------------------- 字符的搜索
20 #include <string>
21 #include <iostream>
22 using namespace std;
23 int main()
24 {
25     string  str("dog bird chicken bird cat");
26 
27     // 字符串查找
28     cout << str.find("bird") << endl;        // 打印 4
29     cout << (int)str.find("pig") << endl;   // 打印 -1
30 
31     // 字符查找
32     cout << str.find('i', 7) << endl;        // 打印 11
33 
34     // 從字符串的 末尾 開始查找字符串
35     cout << str.rfind("bird") << endl;        // 打印 17
36 
37     // 從字符串的 末尾 開始查找字符
38     cout << str.rfind('i') << endl;        // 打印 18
39 
40     // 查找第1個屬於某子串的字符
41     cout << str.find_first_of("13r98") << endl;   // 找到字符 r, 打印 6
42 
43     // 查找第1個不屬於某字符串的字符
44     cout << str.find_first_not_of("dog bird 2009") << endl;   // 找到字符 c , 打印 9
45 
46     // 查找最后一個屬於某子串的字符
47     cout << str.find_last_of("13r98") << endl;  // 字符 r,打印19
48 
49     // 查找最后一個不屬於某字符串的字符
50     cout << str.find_last_not_of("tea") << endl;  // 字符 c, 打印 22
51 
52 
53     return 0;
54 }

 

 

 

字符串的比較
 1 /*    字符串的比較
 2     string 提供了字典式的比較函數 compare ,它的幾個常用函數原型如下:
 3     int  compare(const string& s)    // 將當前字符串與字符串 s 比較,相等返回 0 ,大於 s 返回 1 ,小於 s 返回 -1
 4     int  compare(size_type pos, size_type n, const string& s)    // 將當前字符串從 pos 索引位置開始的 n 個字符構成的字符串與字符串 s 比較,相等返回 0 ,大於 s 返回 1 ,小於 s 返回 -1
 5     int  compare(const char* s)    // 直接將當前字符串與字符串 s 比較。相等返回 0 ,大於 s 返回 1 ,小於 s 返回 -1
 6     
 7     下面的示例程序使用 compare 函數,比較並打印字符串比較結果
 8 */
 9 -------------------------------------------------------- 字符串的比較
10 #include <string>
11 #include <iostream>
12 using namespace std;
13 int main()
14 {
15     string str1("abcdef");
16     string str2("abc");
17     // 相等,打印0
18     cout << str1.compare("abcdef") << endl; 
19 
20     // str1 > str2 , 打印 1
21     cout << str1.compare(str2) << endl;
22 
23     // str1 < "abyz" , 打印 -1
24     cout << str1.compare("abyz") << endl;
25 
26     // str1 的前3個字符 == str2 , 打印 0 
27     cout << str1.compare(0, 3, str2) << endl;
28 
29 
30     return 0;
31 }

 

 

 

string 的其他常用函數用法
 1 /*    其他常用函數
 2     string 字符串還有以下幾個比較常用的函數,如下是他們的使用原型說明。
 3     
 4     size_type  length()    // 字符串的非空字符個數,即字符串的長度
 5     size_type  size()    // 返回字符串的長度
 6     bool  empty()    // 字符串是否為空,空則返回 1,否則返回 0
 7     const  char* c_str()    // 將string 對象轉換為 c 字符數組。 (***)
 8 */
 9 
10 -------------------------------------------------------- string 的其他常用函數用法
11 #include <string>
12 #include <iostream>
13 using namespace std;
14 int main()
15 {
16     string str;
17     // 空字符串,返回 1 
18     cout << str.empty() << endl;
19     str += "1234567";
20     cout << str.empty() << endl;    // 不為空,返回0
21     cout << str.size() << endl;        // 7個字符,返回7
22     cout << str.length() << endl;    // 返回 7
23 
24     // 轉換為字符數組  cArray
25     const char*  cArray=str.c_str();    
26     // 返回字符 3
27     cout << cArray[2] << endl;
28 
29     return 0;
30 }

 

 

 

//  補充幾個其他的用法: 數組方式訪問,或者.at() 函數方式訪問。

#include <string>
#include <iostream>
using namespace std;
int main()
{
    string s1 = "abcdefghijklmnopqrst";
    char chA = s1[3];  // 將第4個字符賦給chA,chA值為d
    char chB = s1.at(5);    // 將第5個字符賦給chB,chB值為f
    char chC = s1[50];  // 數組越界
//    char chD = s1.at(50); // 產生out_of_rang 異常,可以用try,catch捕獲異常
    cout << s1.c_str() << endl;  // 輸出字符串 s1

    return 0;
}



string 的賦值

string &operator=(const string &s);//把字符串s賦給當前的字符串
string &assign(const char *s); //把字符串s賦給當前的字符串
string &assign(const char *s, int n); //把字符串s的前n個字符賦給當前的字符串
string &assign(const string &s);  //把字符串s賦給當前字符串
string &assign(int n,char c);  //用n個字符c賦給當前字符串
string &assign(const string &s,int start, int n);  //把字符串s中從start開始的n個字符賦給當前字符串


string的子串
string substr(int pos=0, int n=npos) const;    //返回由pos開始的n個字符組成的子字符串


string 的 assing 用法:
    // 自己去用代碼實現,熟悉其含義和用法
    string strA("到此一游");
    string strC;    
    strC = strA;
    strC.assign("abcdefghijk");
    strC.assign("baidu");
    strC.assign(strA);
    strC.assign(5,'f');


 string與wstring的區別(*****)
 string是對char*的管理,一個字符占一個字節大小,一個漢字占兩個字節,ASCII編碼。 wstring是對wchar_t*的管理,一個字符占兩個字節大小,一個漢字占兩個字節,Unicode編碼。 wstring的使用方法跟string類似,區別主要在於函數參數char*與函數參數wchar_t* 更多關於 string 與 wstring 的知識,這里不做討論,因為,我也不清楚這方面的知識了。 

 

 

 

 

 

 

 
--------------------------  string 小結
    string 是一個字符序列容器,可自動管理字符的內存分配。教之傳統的 char* 字符數組, string 提供了十分豐富函數用於字符的添加、刪除、替換、查找和比較等。由於篇幅有限,相當多的函數並未列出,讀者可參考 C++STL 的幫助文檔。
    
    string 缺點:在查找方面,string沒有set、map快。這是他的一個缺點。
    string 優點:字符串操作方面,是他的強項。。。。
    
    
    
    
    ============當你們看到這里的時候,我想說,你們有福了。

      我有個《葵花寶典》和你們分享: http://vdisk.weibo.com/s===================
    
      

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM