1. 程式人生 > >C++學習筆記 (三) ---- string 類

C++學習筆記 (三) ---- string 類

引言:在 C++ 中,大大加強了對字串的支援和處理,除了相容 C 語言的字串,還內建了完全可以替換 C語言中的字元陣列和字串指標的 string 類。

使用 string 類需要包含標頭檔案 <string>。

簡單例子:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1;
    string s2 = "c plus plus";
    string s3 = s2;
    string s4 (5, 'c');
    return 0;
}

//變數 s1 只是定義但沒有初始化,編譯器會將預設值賦給 s1,預設值是"",也即空字串。
//變數 s2 在定義的同時被初始化為"c plus plus"。與C風格的字串不同,string 的結尾沒有結束標誌'\0'。
//變數 s3 在定義的時候直接用 s2 進行初始化,因此 s3 的內容也是"c plus plus"。
//變數 s4 被初始化為由 5 個'c'字元組成的字串,也就是"ccccc"。

string 類提供字串長度函式 length(),如:

string s = "hello C++!";
int len = s.length();
cout<<len<<endl;

//輸出結果:10 
//因為沒有結尾的 '\0'字元

轉換為 C語言風格的函式 c_str(),如:

string path = "D:\\ceshi.txt";
FILE *fp = fopen(path.c_str(), "rt");
//使用 C語言的 fopen() 函式開啟檔案,所以要將 string 字串轉換為 C語言風格的字串。

string 類的讀寫或其中字元的訪問都可以看作變數或陣列:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string s;
    cin>>s;  //輸入字串
    cout<<s<<endl;  //輸出字串

    s[5] = '5';   //修改字串中的第六個字元;
    for(int i=0,len=s.length(); i<len; i++){
        cout<<s[i]<<" ";
    }

    return 0;
}

string 類字串拼接

可以直接使用 + 或 += 運算子,而不需要使用 C語言裡的 strcat()、strcpy()、malloc() 等函式,也不用擔心空間溢位。例:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1 = "first ";
    string s2 = "second ";
    char *s3 = "third ";
    char s4[] = "fourth ";
    char ch = '@';

    string s5 = s1 + s2;
    string s6 = s1 + s3;
    string s7 = s1 + s4;
    string s8 = s1 + ch;
    
    cout<<s5<<endl<<s6<<endl<<s7<<endl<<s8<<endl;

    return 0;
}
//執行結果:
first second
first third
first fourth
first @

字串的增刪改查

1、插入字串函式 insert();

string& insert (size_t pos, const string& str);
//pos 表示要插入的位置(下標);str 表示要插入的字串;

2、刪除字串函式 erase();

string& erase (size_t pos = 0, size_t len = npos);
//pos 表示要刪除的子字串的起始下標,len 表示要刪除子字串的長度。如果不指明 len 的話,那麼直接刪除從 pos 到字串結束處的所有字元。

3、提取字串函式 substr();

string substr (size_t pos = 0, size_t len = npos) const;
//pos 為要提取的子字串的起始下標,len 為要提取的子字串的長度。

4、字串查詢函式

    a) find() 函式

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;

//第一個引數為待查詢的子字串,它可以是 string 字串,也可以是C風格的字串。
//第二個引數為開始查詢的位置(下標);如果不指明,則從第0個字元開始查詢。

    b) rfind() 函式

  和 find() 函式相似,只不過第二個引數代表最大的查詢範圍,即該位置以後的就不在查詢範圍內。

    c) finf_first_of() 函式

  用於查詢子字串和字串共同具有的字元在字串中首次出現的位置。

 示例:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string s1, s2, s3;
    s1 = s2 = "1234567890";
    s3 = "aaa";
    s1.insert(5, s3);
    cout<< s1 <<endl;      //12345aaa67890
    s2.insert(5, "bbb");
    cout<< s2 <<endl;      //12345bbb67890

    s1 = s2 = s3 = "1234567890";
    s2.erase(5);
    s3.erase(5, 3);
    cout<< s1 <<endl;    //1234567890
    cout<< s2 <<endl;    //12345
    cout<< s3 <<endl;    //1234590

    s1 = "first second third";
    s2 = s1.substr(6, 6);
    cout<< s1 <<endl;    //first second third
    cout<< s2 <<endl;    //second

    s1 = "first second third";
    s2 = "second";
    int index = s1.find(s2,5);       //Found at index : 6
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;

    s1 = "first second third";
    s2 = "second";
    index = s1.rfind(s2,6);          //Found at index : 6
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;

    s1 = "first second second third";
    s2 = "asecond";
    index = s1.find_first_of(s2);    //Found at index : 3
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}