c 之string類用法詳細總結

2021-12-30 05:46:50 字數 4408 閱讀 8913

標準c++中string類非常強大,合理使用,能極大提高程式設計效率,下面就對string類的用法進行總結。

標頭檔案#include

1) string s; //生成乙個空字串s

2) string s(str) //拷貝建構函式生成str的複製品

3) string s(str,index) //將字串str內「始於位置index」的部分當作字串的初值

4) string s(str,index, n) //將字串str內「始於index且長度頂多n」的部分作為字串的初值

5) string s(cstr) //將c字串作為s的初值

6) string s(chars,chars_len) //將c字串前chars_len個字元作為字串s的初值。

7) string s(n,c) //生成乙個字串,包含n個c字元

8) string s(str.begin(),str.end()) //以區間begin():end() (不包含end())內的字元作為字串s的初值

之後會對相關函式進行講解,如果不想將下面操作函式全部看完,大夥可以找自己感興趣的函式看。

1) =,assign() //賦以新值

2) swap() //交換兩個字串的內容

3)+=,append(),push_back() //在尾部新增字元

4) insert() //插入字元

5) erase() //刪除字元

6) clear() //刪除全部字元

7) replace() //替換字元

8) + //串聯字串

9)==,!=,,>=,compare() //比較字串

10)size(),length() //返回字元數量

11) max_size() //返回字元的可能最大個數

12) empty() //判斷字串是否為空

13) capacity() //返回重新分配之前的字元容量

14) reserve() //保留一定量記憶體以容納一定數量的字元

15) [ ], at() //訪問單一字元

16)>>,getline() //從stream讀取某值

17) << //將謀值寫入stream

18) copy() //將某值賦值為乙個c_string

19) c_str() //將內容以c_string返回

20) data() //將內容以字元陣列形式返回

21) substr() //返回某個子字串

22)查詢函式

23)begin() end() //提供類似stl的迭代器支援

24) rbegin() rend() //逆向迭代器

25) get_allocator() //返回配置器

1)iterator erase(iterator p);//刪除字串中p所指的字元

2)iterator erase(iterator first, iterator last);//刪除字串中迭代器

區間[first,last)上所有字元

3)string& erase(size_t pos = 0, size_t len = npos);//刪除字串中從索引

位置pos開始的len個字元

4)void clear();//刪除字串中所有字元

1)string& replace(size_t pos, size_t n, const char *s);//將當前字串

從pos索引開始的n個字元,替換成字串s

2)string& replace(size_t pos, size_t n, size_t n1, char c); //將當前字串

從pos索引開始的n個字元,替換成n1個字元c

3)string& replace(iterator i1, iterator i2, const char* s);//將當前字串

[i1,i2)區間中的字串替換為字串s

相關函式較多,下面列舉幾個常用的:

1)size_t find (constchar* s, size_t pos = 0) const;

//在當前字串的pos索引位置開始,查詢子串s,返回找到的位置索引,

-1表示查詢不到子串

2)size_t find (charc, size_t pos = 0) const;

//在當前字串的pos索引位置開始,查詢字元c,返回找到的位置索引,

-1表示查詢不到字元

3)size_t rfind (constchar* s, size_t pos = npos) const;

//在當前字串的pos索引位置開始,反向查詢子串s,返回找到的位置索引,

-1表示查詢不到子串

4)size_t rfind (charc, size_t pos = npos) const;

//在當前字串的pos索引位置開始,反向查詢字元c,返回找到的位置索引,

-1表示查詢不到字元

5)size_tfind_first_of (const char* s, size_t pos = 0) const;

//在當前字串的pos索引位置開始,查詢子串s的字元,返回找到的位置索引,

-1表示查詢不到字元

6)size_tfind_first_not_of (const char* s, size_t pos = 0) const;

//在當前字串的pos索引位置開始,查詢第乙個不位於子串s的字元,

返回找到的位置索引,-1表示查詢不到字元

7)size_t find_last_of(const char* s, size_t pos = npos) const;

//在當前字串的pos索引位置開始,查詢最後乙個位於子串s的字元,返回找到的位置索引,-1表示查詢不到字元

8)size_tfind_last_not_of (const char* s, size_t pos = npos) const;

//在當前字串的pos索引位置開始,查詢最後乙個不位於子串s的字元,返回找到的位置索引,-1表示查詢不到子串

1)int compare (conststring& str) const;//將當前字串與字串str比較,

相等返回0,大於str返回1,小於str返回-1

2)int compare (size_tpos, size_t len, const char* s) const; //將當前字串從

pos索引位置開始的len個字元構成的字串與字串s比較,

相等返回0,大於str返回1,小於str返回-1

3)int compare (constchar* s) const; //直接將當前字串與字串s比較,

相等返回0

標準C 中string類的用法總結

相信使用過mfc程式設計的朋友對cstring這個類的印象應該非常深刻吧?的確,mfc中的cstring類使用起來真的非常的方便好用。但是如果離開了mfc框架,還有沒有這樣使用起來非常方便的類呢?答案是肯定的。也許有人會說,即使不用mfc框架,也可以想辦法使用mfc中的api,具體的操作方法在本文最...

c STL之string用法總結

1 標頭檔案 include 2 string類的常用建構函式 string str 構造空的string類物件,即空字串 string str str1 str1 和 str 一樣 string str abc 等價於 str abc string str abc strlen 等價於 abc 存...

標準C 中的string類的用法總結

原文章 相信使用過mfc程式設計的朋友對cstring這個類的印象應該非常深刻吧?的確,mfc中的cstring類使用起來真的非常的方便好用。但是如果離開了mfc框架,還有沒有這樣使用起來非常方便的類呢?答案是肯定的。也許有人會說,即使不用mfc框架,也可以想辦法使用mfc中的api,具體的操作方法...