天天看點

C++STL—string類

string容器

1.1 string容器的基本概念

string容器是一個類 這個容器中有一個指針,指針維護了一個數組

string容器提供copy、find、insert、replace等等功能

使用string容器需要使用string頭檔案,即

# include <string>

1.2 string容器常用操作

1.2.1 string的構造函數
/*
    string();//建立一個空的字元串 例如: string str;
    string(const string& str);//使用一個 string 對象初始化另一個 string 對象
    string(const char* s);//使用字元串 s 初始化
    string(int n, char c);//使用 n 個字元 c 初始化 
*/
string str;
string str1("hello");
string str2(str1);
string str3(5, 'a'); 
cout << str << endl; //空字元串
cout << str1 << endl; //hello
cout << str2 << endl; //hello
cout << str3 << endl; //aaa

//注意:每一行代碼是一個單獨的例子
           
1.2.2 string基本指派操作
/*
    string& operator=(const char* s);//char*類型字元串 指派給目前的字元串
    string& operator=(const string &s);//把字元串 s 賦給目前的字元串
    string& operator=(char c);//字元指派給目前的字元串
    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 str;
string str1("helloworld");

str = "hehe"; //hehe
str = str1; // helloworld
str = 'a'; // a
str.assign("abcdefg"); //abcdefg
str.assign("abcdefg", 3); // abc
str.assign(str1);// helloworld
str.assign(5, 'c'); //ccccc
str.assign(str1, 0, 4); //hell

//注意:每一行代碼是一個單獨的例子
           
1.2.3 string中内容的通路
/*
    char& operator[](int n);//通過[]方式取字元
    char& at(int n);//通過 at 方法擷取字元
    通過疊代器通路
*/
           

(1) 通過下标通路

int main() {
	string str = "abcdefg"; 
    
	cout << str[0] << endl;  // a
	
	return 0;
}
           
int main() {
	string str = "abcdefg"; 
    
	cout << str.at(2) << endl;  // c
	
	return 0;
}
           
int main() {
	string str = "abcdefg";   
	string::iterator it = str.begin();
    
    cout << *(it + 3) << endl; //d
	
	return 0;
}
           
1.2.4 string的拼接操作
/*
    string& operator+=(const string& str);//重載+=操作符
    string& operator+=(const char* str);//重載+=操作符
    string& operator+=(const char c);//重載+=操作符
    string& append(const char *s);//把字元串 s 連接配接到目前字元串結尾
    string& append(const char *s, int n);//把字元串 s 的前 n 個字元連接配接到目前字元串結尾
    string& append(const string &s);//同 operator+=()
    string& append(const string &s, int pos, int n);//把字元串 s 中從 pos 開始的 n 個字元連接配接到目前字元串結尾
    string& append(int n, char c);//在目前字元串結尾添加 n 個字元 
*/

string str1 = "abc", str2 = "def", str3, str4;

str3 = str1 + str2; // abcdef
str3 = str1 + "666"; //abc666
str3 = str1 + 'a'; //abca
str3.append(str1); //abc
str3.append("abcdef", 2); //ab
str2.append(str1); //defabc
str3.append(str1, 1, 2); //bc
str4.append(4, 'x'); //xxxx

//注意:每一行代碼是一個單獨的例子
           
1.2.5 string的查找
/*
    int find(const string& str, int pos = 0) const; //查找str第一次出現位置, 從pos開始查找(pos可以省略,預設從0開始)
    int find(const char* s, int pos = 0) const; //查找 s 第一次出現位置,從 pos開始查找
    int find(const char* s, int pos, int n) const; //從 pos 位置查找 s 的前 n 個字元第一次位置
    int find(const char c, int pos = 0) const; //查找字元 c 第一次出現位置
    int rfind(const string& str, int pos = npos) const;//查找 str 最後一次位置, 從 pos 開始查找
    int rfind(const char* s, int pos = npos) const;//查找 s 最後一次出現位置,從pos 開始查找最後一次位置
    int rfind(const char c, int pos = 0) const; //查找字元 c 最後一次出現位置
*/

//常用:
string str = "Thank you for your smile";
string str2 = "you";
string str3 = "me";

cout << str.find(str2) << endl; //6
cout << str.find(str2, 8) << endl; // 14
cout << str.find(str3) << endl; // 常數string::npos用作為find函數失配時的傳回值,為-1或者4294967295

//注意:每一行代碼是一個單獨的例子
           
1.2.6 string的替換
/*
    string& replace(int pos, int n, const string& str); //替換從 pos 開始 n 個字元為字元串 str
    string& replace(int pos, int n, const char* s); //替換從 pos 開始的 n 個字元為字元串 s
    str.replace(it1, it2, str2)把str的疊代器[it1, it2)範圍的子串替換為str2
*/

string str = "Maybe you will turn around";
string str2 = "will not";
string str3 = "surely";

string::iterator it = str.begin();
cout << str.replace(10, 4, str2) << endl; // Maybe you will not turn around
cout << str.replace(it + 6, it + 9, str3) << endl; // Maybe surely will turn around

//注意:每一行代碼是一個單獨的例子
           
1.2.7 字元串的比較
/*
    compare 函數在>時傳回 1,<時傳回 -1,==時傳回 0。
    比較區分大小寫,比較時參考字典順序,排越前面的越小。
    大寫的 A 比小寫的 a 小。 比較規則是字典序
    int compare(const string &s) const;//與字元串 s 比較
    int compare(const char *s) const;//與字元串 s 比較
*/

string str1 = "aa";
string str2 = "aa";
string str3 = "xyz";

cout << str1.compare(str2) << endl; // -1  因為str1 < str2 
cout << str3.compare(str1) << endl; // 1   因為str3 > str1
cout << str1.compare(str2) << endl; // 0   因為str1 == str2
           
1.2.8 字元串的長度
/*
	length()/size()都傳回string的長度,即存放的字元數
*/
string str = "abcdef";
str.size(); //6
str.length(); //6
           
1.2.9 字元串的子串
/*
	string substr(int pos = 0, int n = npos) const;//傳回由 pos 開始的 n 個字元組成的字元串
*/

string str1 = "Thank you for your smile";

string s = str1.substr(0, 5); // Thank
str1.substr(6, 3); // you
           
1.2.10 string::npos
string::npos是一個常數,其本身的值為-1,但由于是unsigned_int類型,是以實際上也可以被認為是unsigned_int類型的最大值。
這個值在string成員函數中作len(或subblen)參數的值時,表示“知道字元串結束”;
作為傳回值,它常用語表示沒有比對
           
1.2.11 字元串的插入
/*
    string& insert(int pos, const char* s); //在pos的位置 插入字元串常量
    string& insert(int pos, const string& str); //在pos的位置,插入字元串對象
    string& insert(int pos, int n, char c);//在指定位置插入 n 個字元 c
    insert(it1, it2, it3); it1為原字元串的欲插入位置,it2和it3為待插入字元串的首位疊代器,用來表示串[it2, it3)将被插在			it1上
*/
string str1 = "Thank you";
string str2 = "Love you";

cout << str1.insert(1, "aaa") << endl; //Taaahank you
cout << str2.insert(4, str1) << endl; //LoveThank you you
cout << str1.insert(2, 5, 'c'); //Thcccccank you

str1.insert(str1.begin() + 2, str2.begin() + 1, str2.begin() + 5); 
cout << str1 << endl; //Thove ank you

//注意:每一行代碼是一個單獨的例子
           
1.2.12 字元串的删除
/*
	1、删除單個元素:
		str.erase(it)用于删除單個元素,it為需要删除的元素的疊代器
	2、删除一個區間内的所有元素:
		str.erase(first, last), 其中first為需要删除的區間的起始疊代器,last為末尾疊代器的下一個位址,即[first,last)
		string& erase(int pos, int n = npos);//删除從 Pos 開始的 n 個字元	
*/

string str = "abcdefg";

str.erase(str.begin() + 4); //abcdfg
str.erase(str.begin() + 1, str.begin() + 4); //aefg
str.erase(2, 4); //abg
           
1.2.13 字元串的清除
/*
	str.clean()用以清空string中的資料
*/
string str = "abcdefg";
str.clean();
cout << str.length() << endl; //0
           
1.2.14 string和c風格的轉換
void main(){
    string str1; //對象
    char* str2 = "hello str2";
    
    //将char* 轉換成 string(直接完成)
    str1 = str2;
    cout << str1 << endl; // hello str2
    
    string str3 = // hello str2
    //不能直接将string轉換成char* 必須借助string中的c_str方法完成
    //char* str4 = str3; 錯誤
    
    char* str4 = const_cast<char *>(str3.c_str());
    //const_cast < type-id > ( expression ) 去掉const屬性
    cout << str4 << endl; // hello str3
    
    return 0;
}
           

繼續閱讀