人見人愛的string容器

內容思維導圖:

人見人愛的string容器

一、string基本概念:

1、本質

string是c++風格的字串,而string本質上是一個類。

2、string和char *的區別

-char *是一個char型別的指標型別

-string是一個類,類內部封裝了char*,是一個char

型的容器。string類內部封裝了很多成員函式,比如:查詢find,複製copy,刪除delete,替換replace,插入insert;string管理char

所分配的記憶體,不用擔心複製越界和取值越界等,由類內部進行負責

二、string建構函式:

建構函式原型:

string();//建立一個空的字串,例如string str

string(const char* s);//使用字串s初始化

string(const string& str);//使用一個string物件初始化另外一個string物件,就是複製建構函式

string(int n, char c);//使用n個字元c初始化

程式碼應用舉例:

#include #include using namespace std;void test(){   string s1;   const char *str = “txp”;   string s2(str);   cout << “s2= ”<

輸出結果:

root@txp-virtual-machine:/home/txp/test2# 。/a。outs2= txps3= txps4= aaaaaaaaaa

三、string賦值操作:

實現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);//把字串的前n個字元賦給當前的字串

string& assign(const string &s);//把字串s賦給當前字串

string& assign(int n, char c);//用n個字元c賦給當前字串

程式碼應用:

#include #include using namespace std;void test(){   string str1;   str1 = “txp”;   cout<<“str1= ”<

結果輸出:

root@txp-virtual-machine:/home/txp/test2# 。/a。outstr1= txpstr2 = txpstr3= astr4= txp like cppstr5= txp lstr6= txp lstr7= wwwwwwwwww

四、string字串拼接:

實現字串末尾拼接字串函式原型:

string& operator+=(const char* str);//過載+=運算子

string& operator+=(const char c);//過載+=運算子

string& operator+=(const string& str);//過載+=運算子

string& append(const char *s);//把字串s連線到當前字串結尾

string& append(const char *s,int n);//把字串s的前n個字元連線到當前字串結尾

string& append(const string &s);//同operator+=(const string& str)

string& append(const string &s,int pos,int n);//字串s從pos開發的n個字元連線到字串結尾

程式碼應用:

#include #include using namespace std;void test(){   string str1 = “txp”;   str1 += “xiaoping”;   cout <<“str1= ”<

結果輸出:

root@txp-virtual-machine:/home/txp/test2# 。/a。outstr1= txpxiaopingstr1= txpxiaoping:str1= txpxiaoping:cppstr3= Ilovestr3= Ilovegamestr3= Ilovegamecppstr3= Ilovegamecppcpp

五、string查詢和替換:

實現字串的查詢和替換函式原型:

int find(const string& str,int pos = 0)const;//查詢str第一次出現位置從pos開始查詢

int find(const char* s,int pos =0);//查詢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* s,int pos, int n)const;//從pos查詢s的前n個字元最後一次位置

int rfind(const char c ,int pos = 0)const;//查詢字元c最後一次出現位置

string& replace(int pos, int n,const string& str);//替換從pos開始n個字元為字串str

string& replace(int pos, int n, const char* s);//替換從pos開始的n個字元為字串s

程式碼應用:

#include #include using namespace std;void test(){  string str1 = “abcd”;  int pos = str1。find(“bc”);  if(pos)  {     cout<< “ find the word”<

結果輸出:

root@txp-virtual-machine:/home/txp/test2# 。/a。out find the wordpos= 1pos= 2str2= a1111

六、string字串比較:

比較方式:字串比較是按字元的ASCII碼進行對比的:

= : 返回 0

: 返回 1

< : 返回 -1

函式原型:

int copare(const string &s)const;//與字串s比較

int compare(const char *s)const;//與字串s比較

程式碼應用:

#include #include using namespace std;void test(){  string str1 = “txp”;  string str2 = “linux”;  if(str1。compare(str2)==0)  {     cout<<“str1 = str2 ”<0)  {     cout<<“str1 > str2 ”<

結果輸出:

root@txp-virtual-machine:/home/txp/test2# 。/a。outstr1 > str2

七、string字元存取:

實現字元存取函式原型:

char& operator[](int n);//透過[]方式取字元

char& at(int n);//透過at方法獲取字元

程式碼應用:

#include #include using namespace std;void test(){  string str = “linux”; //透過[]訪問單個字元  for(int i = 0;i< str。size();i++)  {     cout<

輸出結果:

root@txp-virtual-machine:/home/txp/test2# 。/a。outl i n u x l i n u x str = xinuxstr = xxnux

八、string插入和刪除:

實現string插入和刪除的函式原型:

string& insert(int pos,const char * s);//插入字串

string& insert(int pos, const string&str);//插入字串

string& insert(int pos, int n,char c);//在指定位置插入n個字元c

string& erase(int pos, int n = npos);//刪除從pos開始的n個字元

程式碼應用:

#include #include using namespace std;void test(){  string str = “linux”;  str。insert(1,“111”);  cout<<“str = ”<

結果輸出:

root@txp-virtual-machine:/home/txp/test2# 。/a。outstr = l111inuxstr = linux

九:string子串

實現從string字串中獲得子串的函式原型:

string substr(int pos =0 , int n =npos)const;//返回由pos開始的n個字元組成的字串

程式碼應用:

#include #include using namespace std;void test(){  string str = “linux”;  string substr = str。substr(1,3);  cout<<“substr =  ”<

結果輸出:

root@txp-virtual-machine:/home/txp/test2# 。/a。outsubstr =  inulin

我是txp,一個只專注於乾貨分享的博主,歡迎隨時來撩我,我們下期見!

更多精彩內容,可以微信搜尋:txp玩linux