《C++ string類》

C++標準庫中string類以類型的形式對字符串進行封裝,使得它除了像一個存儲字符的容器外,更加包含了字符序列的處理操作。

string類所有函數


string類的所有成員函數
函數名稱 實現功能
構造函數 產生或者複製字符串
析構函數 銷燬字符串
assign,= 賦值
Swap 交換兩個字符串的內容
append(),push_back(),+= 添加字符
insert() 插入字符
erase() 刪除字符
clear() 移除全部字符
resize() 改變字符數量
replace() 替換字符
+ 串聯字符串
compare(),==,!,<,<=,>,>= 比較字符串內容
size(),length 返回字符數量
max_size() 返回字符的最大可能個數
empty() 判斷字符串是否爲空
capacity() 返回重新分配之前的字符容量
reserve() 保留內存以存儲一定數量的字符
[],at() 存取單一字符
>>,geline() 從stream中讀取某值
<< 將值寫入stream
copy() 將內容複製爲一個C-string
c_str() 將內容以C-string形式返回
data() 將內容以字符數組形式返回
substr() 返回子字符串
find() 搜尋某子字符串或字符
begin(),end() 提供正向迭代器支持
rbrgin(),rend() 提供逆向迭代器支持
get_allocator() 返回配置器

 

string類包含頭文件


#include <string>

聲明一個string類對象

string Str;

既然是一個類,就有構造函數和析構函數。上面的聲明沒有傳入參數,所以直接使用了string的默認構造函數,目的是初始化爲一個空字符串。string類的構造函數和析構函數:

  1. string s; //生成一個空字符串s
  2. string s(str) //拷貝構造函數 生成str的複製品
  3. string s(str,stridx) //將字符串str內“始於位置stridx”的部分當作字符串的初值
  4. string s(str,stridx,strlen) //將字符串str內“始於stridx且長度頂多strlen”的部分作爲字符串的初值
  5. string s(cstr) //將C字符串作爲s的初值
  6. string s(chars,chars_len) //將C字符串前chars_len個字符作爲字符串s的初值。
  7. string s(num,c) //生成一個字符串,包含num個c字符
  8. string s(beg,end) //以區間beg;end(不包含end)內的字符作爲字符串s的初值
  9. s.~string() //銷燬所有字符,釋放內存

最常用的是:

string s(str) //字符串的初始值爲str

直接將字符串作爲構造函數的參數。

注意:不能使用字符或者整數去初始化字符串。例如:

string str('x');

string字符串的輸入與輸出

示例代碼

  1. /************************************************************************
  2. <*@創建者:OYXL
  3. <*@創建時間:2018/6/8
  4. <*@程序功能:用於string的輸入與輸出
  5. <*@注意:cin輸入的字符串中不能有空格,用getline()函數可以解決
  6. ************************************************************************/
  7. #include "iostream"
  8. #include "string"
  9. using namespace std;
  10. int main()
  11. {
  12. char s1[20];
  13. string s2;
  14. cout<<"input a string: ";
  15. cin>>s1;
  16. cout<<"input a string again: ";
  17. cin>>s2;
  18. cout<<"s1 is :"<<s1<<endl;
  19. cout<<"s2 is :"<<s2<<endl;
  20. system("pause");
  21. return 0;
  22. }

顯示結果

C++ cin不支持輸入空格,用getline()函數可以解決,下面是博客鏈接:

https://blog.csdn.net/EXLsunshine/article/details/28440629

 

string的基本操作


賦值和拼接

string類的賦值和拼接都是對字符串的賦值操作。賦值是將原有的字符串捨棄,用新的字符串代替。拼接則是不捨棄原有的字符串,僅將新的字符串連接在原有的字符串的末尾。

1、賦值

=和assign() 函數都可以用來給字符串進行賦值

assign() 函數的函數聲明:

  1. //用c類型字符串s賦值
  2. string &assign(const char *s);
  3. //用c類型字符串s開始的n個字符賦值
  4. string &assign(const char *s,int n);
  5. //把型字符串s賦給當前字符串
  6. string &assign(const string &s);
  7. //用n個字符c賦值給當前字符串
  8. string &assign(int n,char c);
  9. //把字符串s中從start開始的n個字符賦給當前字符串
  10. string &assign(const string &s,int start,int n);
  11. //把first和last迭代器之間的部分賦給字符串
  12. string &assign(const_iterator first,const_iterator last);

示例代碼:

  1. # include <iostream>
  2. # include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string str1 = "hello world";
  7. string str2 ("see you again");
  8. string str3,str4;
  9. str3.assign(str2,3,6);
  10. str4.assign(str2,3,string::npos);
  11. cout<<"str4 is :"<<str4<<endl;
  12. str4.assign("love");
  13. cout<<"str4 is :"<<str4<<endl;
  14. str4.assign("nice",5);//大於等於4都可以
  15. cout<<"str4 is :"<<str4<<endl;
  16. str4.assign(5,'x');
  17. cout<<"str1 is :"<<str1<<endl;
  18. cout<<"str2 is :"<<str2<<endl;
  19. cout<<"str3 is :"<<str3<<endl;
  20. cout<<"str4 is :"<<str4<<endl;
  21. system("pause");
  22. return 0;
  23. }

顯示結果

2、拼接

+=,append(),push_back() 都可以用來拼接字符串

示例代碼

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string s,s1(" world");
  7. s+="hello";
  8. cout<<s<<endl;
  9. s+=s1;
  10. cout<<s<<endl;
  11. system("pause");
  12. return 0;
  13. }

顯示結果

append()函數聲明

  1. //把c類型字符串s連接到當前字符串結尾
  2. string &append(const char *s);
  3. //把c類型字符串s的前n個字符連接到當前字符串結尾
  4. string &append(const char *s,int n);
  5. //將string對象s的字符串連接到當前字符串的末尾
  6. string &append(const string &s);
  7. //在當前字符串結尾添加n個字符c
  8. string &append(int n,char c);
  9. //把迭代器first和last之間的部分連接到當前字符串的結尾
  10. string &append(const_iterator first,const_iterator last);
  11. //把字符串s中從pos開始的n個字符連接到當前字符串的結尾
  12. string &append(const string &s,int pos,int n);

比較字符串

==,!=,<,<=,>,>=,compare() 函數用於進行比較字符串操作。比較方法是依次從字符串的初始位置兩兩比較對應位置字符的大小,直到有不同的字符或字符串結束爲止。

示例代碼

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string s1("hello"),s2("world");
  7. cout<<"s1<s2 is ";
  8. cout<<((s1<s2)?"true":"false")<<endl;
  9. cout<<"s1>s2 is ";
  10. cout<<((s1>s2)?"true":"false")<<endl;
  11. cout<<"s1<=s2 is "<<endl;
  12. cout<<((s1<=s2)?"true":"false")<<endl;
  13. cout<<"s1>=s2 is "<<endl;
  14. cout<<((s1>=s2)?"true":"false")<<endl;
  15. cout<<"s1!=s2 is "<<endl;
  16. cout<<((s1!=s2)?"true":"false")<<endl;
  17. system("pause");
  18. return 0;
  19. }

因爲第一個字符就是不同的,所以就比較第一個字符大小。

顯示結果

compare() 函數聲明

  1. //比較當前string對象和s的大小
  2. int compare(const string &s) const;
  3. int compare(const char *s) const;
  4. //比較當前字符串從pos開始的n個字符組成的字符串與s的大小
  5. int compare(int pos,int n,const string &s) const;
  6. int compare(int pos,int n,const char *s) const;
  7. //比較當前字符串從pos開始的n個字符組成的字符串與s中pos2開始的n2個字符的字符串的大小
  8. int compare(int pos,int n,const char *s,int pos2) const;
  9. int compare(int pos,int n,const char *s,int pos2,int n2) const;

子串

substr()函數用於獲取字符串對象的子字符串。該函數返回string對象的某個子串。其中pos爲子串的初始位置,如果不設置,則默認爲從0開始,npos爲子串的長度。

示例代碼

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string s("hello world!");
  7. string s1,s2;
  8. s1=s.substr(6,5);
  9. s2=s.substr(0,5);
  10. cout<<"s1 : "<<s1<<endl;
  11. cout<<"s2 : "<<s2<<endl;
  12. system("pause");
  13. return 0;
  14. }

顯示結果

交換字符串

swap() 函數用於交換字符串

示例代碼

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string str1 = "see you again";
  7. string str2 ("nice to meet you");
  8. str2.swap(str1);
  9. cout<<str1<<endl;
  10. cout<<str2<<endl;
  11. system("pause");
  12. return 0;
  13. }

顯示結果

字符插入

 insert()函數用於插入一個或者多個字符,需要注意的是string對象中字符的初始位置從0開始。

示例代碼

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. string s1("hello world!");
  7. cout<<"s1 : "<<s1<<endl;
  8. s1.insert(6,"my ");
  9. cout<<"s1 : "<<s1<<endl;
  10. string s2("program ");
  11. s1.insert(9,s2);
  12. cout<<"s1 : "<<s1<<endl;
  13. system("pause");
  14. return 0;
  15. }

顯示結果

替換字符

replace() 函數功能是用一個字符串將當前字符串中的某個子串做替換。如果替換字符串與子串的長度不等,則還需要對字符數組進行移動。當字符串中的全部字符被替換成了另一個字符串時,替換字符就變成了賦值。

查找字符串

1、find()函數

find()函數是按字符串從左往右的順序進行查找。默認查找的初始位置爲字符串首字符。一旦查找到當前字符串有匹配的字符或者字符串是,就返回該字符或字符串的首字符下標。

2、rfind()函數

rfind()函數是按字符串逆序查找,從左往右開始查找。

3、find_first_of()函數

find_first_of()函數是查找在字符串中第一個與str中的某個字符匹配的字符,返回它的位置。

返回字符數量

size(),length()這兩個函數會返回string類型對象中的字符個數,且它們的執行效果相同。

max_size()函數返回string類型對象最多包含的字符數。一旦程序使用長度超過max_size()的string操作,編譯器會拋出length_error異常。

capacity()函數返回在重新分配內存之前,string類型對象所能包含的最大字符數。

reserve()函數可以爲string類型對象重新分配內存。重新分配的大小由其參數決定。reserve()的默認參數爲0。

判斷字符串是否爲空

empty()

刪除字符

erase()函數和clear()函數

參考:

https://blog.csdn.net/fenxinzi557/article/details/51457829

《C++入門很簡單》

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章