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類的構造函數和析構函數:
- string s; //生成一個空字符串s
- string s(str) //拷貝構造函數 生成str的複製品
- string s(str,stridx) //將字符串str內“始於位置stridx”的部分當作字符串的初值
- string s(str,stridx,strlen) //將字符串str內“始於stridx且長度頂多strlen”的部分作爲字符串的初值
- string s(cstr) //將C字符串作爲s的初值
- string s(chars,chars_len) //將C字符串前chars_len個字符作爲字符串s的初值。
- string s(num,c) //生成一個字符串,包含num個c字符
- string s(beg,end) //以區間beg;end(不包含end)內的字符作爲字符串s的初值
- s.~string() //銷燬所有字符,釋放內存
最常用的是:
string s(str) //字符串的初始值爲str
直接將字符串作爲構造函數的參數。
注意:不能使用字符或者整數去初始化字符串。例如:
string str('x');
string字符串的輸入與輸出
示例代碼
- /************************************************************************
- <*@創建者:OYXL
- <*@創建時間:2018/6/8
- <*@程序功能:用於string的輸入與輸出
- <*@注意:cin輸入的字符串中不能有空格,用getline()函數可以解決
- ************************************************************************/
- #include "iostream"
- #include "string"
- using namespace std;
-
- int main()
- {
- char s1[20];
- string s2;
- cout<<"input a string: ";
- cin>>s1;
- cout<<"input a string again: ";
- cin>>s2;
- cout<<"s1 is :"<<s1<<endl;
- cout<<"s2 is :"<<s2<<endl;
- system("pause");
- return 0;
- }
顯示結果
C++ cin不支持輸入空格,用getline()函數可以解決,下面是博客鏈接:
https://blog.csdn.net/EXLsunshine/article/details/28440629
string的基本操作
賦值和拼接
string類的賦值和拼接都是對字符串的賦值操作。賦值是將原有的字符串捨棄,用新的字符串代替。拼接則是不捨棄原有的字符串,僅將新的字符串連接在原有的字符串的末尾。
1、賦值
=和assign() 函數都可以用來給字符串進行賦值
assign() 函數的函數聲明:
- //用c類型字符串s賦值
- string &assign(const char *s);
- //用c類型字符串s開始的n個字符賦值
- string &assign(const char *s,int n);
- //把型字符串s賦給當前字符串
- string &assign(const string &s);
- //用n個字符c賦值給當前字符串
- string &assign(int n,char c);
- //把字符串s中從start開始的n個字符賦給當前字符串
- string &assign(const string &s,int start,int n);
- //把first和last迭代器之間的部分賦給字符串
- string &assign(const_iterator first,const_iterator last);
示例代碼:
- # include <iostream>
- # include <string>
- using namespace std;
- int main()
- {
- string str1 = "hello world";
- string str2 ("see you again");
- string str3,str4;
-
- str3.assign(str2,3,6);
- str4.assign(str2,3,string::npos);
- cout<<"str4 is :"<<str4<<endl;
- str4.assign("love");
- cout<<"str4 is :"<<str4<<endl;
- str4.assign("nice",5);//大於等於4都可以
- cout<<"str4 is :"<<str4<<endl;
- str4.assign(5,'x');
- cout<<"str1 is :"<<str1<<endl;
- cout<<"str2 is :"<<str2<<endl;
- cout<<"str3 is :"<<str3<<endl;
- cout<<"str4 is :"<<str4<<endl;
- system("pause");
- return 0;
- }
顯示結果
2、拼接
+=,append(),push_back() 都可以用來拼接字符串
示例代碼
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- string s,s1(" world");
- s+="hello";
- cout<<s<<endl;
- s+=s1;
- cout<<s<<endl;
- system("pause");
- return 0;
- }
顯示結果
append()函數聲明
- //把c類型字符串s連接到當前字符串結尾
- string &append(const char *s);
- //把c類型字符串s的前n個字符連接到當前字符串結尾
- string &append(const char *s,int n);
- //將string對象s的字符串連接到當前字符串的末尾
- string &append(const string &s);
- //在當前字符串結尾添加n個字符c
- string &append(int n,char c);
- //把迭代器first和last之間的部分連接到當前字符串的結尾
- string &append(const_iterator first,const_iterator last);
- //把字符串s中從pos開始的n個字符連接到當前字符串的結尾
- string &append(const string &s,int pos,int n);
比較字符串
==,!=,<,<=,>,>=,compare() 函數用於進行比較字符串操作。比較方法是依次從字符串的初始位置兩兩比較對應位置字符的大小,直到有不同的字符或字符串結束爲止。
示例代碼
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- string s1("hello"),s2("world");
- cout<<"s1<s2 is ";
- cout<<((s1<s2)?"true":"false")<<endl;
-
- cout<<"s1>s2 is ";
- cout<<((s1>s2)?"true":"false")<<endl;
- cout<<"s1<=s2 is "<<endl;
- cout<<((s1<=s2)?"true":"false")<<endl;
- cout<<"s1>=s2 is "<<endl;
- cout<<((s1>=s2)?"true":"false")<<endl;
- cout<<"s1!=s2 is "<<endl;
- cout<<((s1!=s2)?"true":"false")<<endl;
- system("pause");
- return 0;
- }
因爲第一個字符就是不同的,所以就比較第一個字符大小。
顯示結果
compare() 函數聲明
- //比較當前string對象和s的大小
- int compare(const string &s) const;
- int compare(const char *s) const;
- //比較當前字符串從pos開始的n個字符組成的字符串與s的大小
- int compare(int pos,int n,const string &s) const;
- int compare(int pos,int n,const char *s) const;
- //比較當前字符串從pos開始的n個字符組成的字符串與s中pos2開始的n2個字符的字符串的大小
- int compare(int pos,int n,const char *s,int pos2) const;
- int compare(int pos,int n,const char *s,int pos2,int n2) const;
子串
substr()函數用於獲取字符串對象的子字符串。該函數返回string對象的某個子串。其中pos爲子串的初始位置,如果不設置,則默認爲從0開始,npos爲子串的長度。
示例代碼
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- string s("hello world!");
- string s1,s2;
- s1=s.substr(6,5);
- s2=s.substr(0,5);
- cout<<"s1 : "<<s1<<endl;
- cout<<"s2 : "<<s2<<endl;
- system("pause");
- return 0;
- }
顯示結果
交換字符串
swap() 函數用於交換字符串
示例代碼
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- string str1 = "see you again";
- string str2 ("nice to meet you");
-
- str2.swap(str1);
- cout<<str1<<endl;
- cout<<str2<<endl;
- system("pause");
- return 0;
- }
顯示結果
字符插入
insert()函數用於插入一個或者多個字符,需要注意的是string對象中字符的初始位置從0開始。
示例代碼
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- string s1("hello world!");
- cout<<"s1 : "<<s1<<endl;
- s1.insert(6,"my ");
- cout<<"s1 : "<<s1<<endl;
- string s2("program ");
- s1.insert(9,s2);
- cout<<"s1 : "<<s1<<endl;
- system("pause");
- return 0;
- }
顯示結果
替換字符
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++入門很簡單》