C++中的string詳解
阿新 • • 發佈:2018-12-09
- 標準庫型別string表示可變長的字元序列,為了在程式中使用string型別,我們必須包含標頭檔案: #include <string>
- 宣告一個字串
- 宣告一個字串有很多種方式,具體如下:
1 string s;//呼叫預設建構函式,s為一個空字串 2 string s(str);//等價於string s = str;呼叫拷貝建構函式,s是str的備份 3 string s(str,strindex);//將字串str內始於strindex位置的部分當作s的初始值 4 eg.string str = "123456789"; 5 string s(str,3);//s的初值為str由位置3開始的字串,即456789 6 string s(str,stridx,strlen);// 將字串str由stridx位置起始且長度為strlen的部分最為s的初值,如果strlen大於最大長度,則只擷取字串最大長度 7 eg.string s(str,3,10);//s=456789,由位置3開始,擷取長度為10的部分,由於str剩餘部分長度小於10,則擷取str剩餘最大長度 8 string s(cstr);//將C風格字串作為s的初值 9 eg.string s("hello");//s的初值為hello 10 string s(cstr,length);//將C風格字串的length長度部分作為s的初值 11 eg.string s("hello",2);//s="he" 12 string s(num,c);//生成一個字串,包含num個c字元 13 eg.string s(10,'c');//s的初值為“cccccccccc”
- 字串操作函式
- c++字串的操作函式很多,這裡把常用的羅列出來
1 =、assign()//用於賦予新值,assign函式用於將一個字串的部分內容賦值給另一個string物件 2 eg.string s1 = "hello"; 3 string s2; 4 s2.assign(s1,0,3);//s2的值為“hel” 5 6 swap() //交換兩個字串的內容 7 eg.string s1 = "hello"; 8 string s2 = "world"; 9 swap(s1,s2);//swap函式將s1和s2的內容交換,現在s1="world",s2="hello" 10 11 +=、append()、push_back()//在字串尾部追加內容,"+="可追加string物件,字元以及C風格字串,append函式則可以追加string物件和C風格字串,push_back函式則只能追加字元 12 eg.string s1 = "hello"; 13 string s2 = " world"; 14 s1 += s2;//正確,s1的值為”hello world“ 15 s1 +="world";// 正確,s1的值為"hello world" 16 s1 +='c'; //正確,s1的值為"helloc" 17 18 s1.append(s2);//正確,s1的值為"hello world" 19 s1.append(" world");//正確,s1的值為"hello world" 20 s1.append('c');//錯誤,append函式不支援追加字元 21 22 s1.push_back(s2);//錯誤 23 s1.push_back("world");//錯誤 24 s1.push_back('c');//正確 25 26 27 insert()//用於插入字串 28 eg.string s1 = "hello"; 29 s1.insert(0,"world ");//s1的值為world hello 30 31 erase()//用於刪除字元的 32 eg.string str("This is an example phrase."); 33 string::iterator it;//迭代器 34 35 str.erase(10,8);//str的值為"This is an phrase.",刪除了從位置10開始的8個字元 36 37 it = str.begin()+9;//迭代器位置為9 38 str.erase(it);//刪除了從it迭代器位置處的一個字元,str="This is a phrase." 39 40 str.erase(str.begin()+5,str.end()-7);//刪除兩個引數之間的所有字元,str="This phrase." 41 42 43 clear()函式和~string()//都是用來刪除全部字元的 44 eg.str.clear();//刪除str的全部字元,此時str為一個空串 45 str.~string();//銷燬所有字元,釋放記憶體 46 47 replace()函式,用於替換字元 48 eg.1.string line = "[email protected] [email protected] a test string!"; 49 line = line.replace(line.find("@"),1,"");//將line中從find的@位置開始替換一個長度的字元為"" 結果為this [email protected] a test string! 50 51 ==、!=、<、<=、>、>=、compare()//比較字串 52 eg.string s1 = "haha"; 53 string s2 = "haha"; 54 if(s1.compare(s2) == 0){ 55 cout << "相等" << endl; 56 } 57 58 size()函式和length()函式,返回字串的字元數 59 eg.string str = "haha"; 60 str.size() 等於 str.length(),值均為4 61 62 empty()//判斷字串是否為空 63 64 下標法str[index]或者str.at(index)獲取字串內指定位置的字元 65 66 data()函式,將內容以字元陣列的形式返回
- C++字串和C字串的轉換
- C++提供的由C++字串得到對應的C_string的方法是使用data()、c_str()和copy(),其中,data()以字元陣列的形式返回字串內容,但並不新增'\0'.
- c_str()返回一個以'\0'結尾的字元陣列
- copy()則把字串的內容複製或寫入已有的c_string或字元陣列內
- 元素存取
- 我們可以使用下標操作符[]和函式at()來對字串的字元進行訪問,但是應該注意的是下標操作符並不會檢查索引是否有效,如果索引失效,會引起未定義的行為
- at()函式則會檢查索引,如果索引失效會丟擲out_of_range異常
- 注意,操作符可取到字串尾部的'\0'字元
已知類string的原型為:
1 class String 2 { 3 public: 4 String(const char *str = NULL);//普通建構函式 5 String(const String &other);//拷貝建構函式 6 ~String(void);//解構函式 7 8 private: 9 char *m_data;//用於儲存字串 10 };
編寫上述三個函式的實現:
1 //普通建構函式 2 String:String(const char *str) 3 { 4 if(str == NULL){ 5 m_data = new char[1]; 6 *m_data = '\0'; 7 }else{ 8 int length = strlen(str); 9 m_data = new char[length+1]; 10 strcpy(m_data,str); 11 } 12 } 13 14 //解構函式 15 String::~String(void) 16 { 17 delete []m_data; 18 } 19 20 //拷貝建構函式 21 String::String(const String &other) 22 { 23 int length = strlen(other.m_data); 24 m_data = new char[length+1]; 25 strcpy(m_data,other.m_data); 26 }