stl String常用函式

2021-07-29 12:25:06 字數 955 閱讀 4380

原文:

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

*const char *c_str()const;//返回乙個以null終止的c字串,用printf(()輸出時需要

*int size()const;        //返回當前字串的大小 int length()const;       //返回當前字串的長度 bool empty()const;       

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

**iterator erase(iterator it);//刪除it指向的字元,返回刪除後迭代器的位置

**int find(char c, int pos = 0) const;//從pos開始查詢字元c在當前字串的位置

**int find(const char *s, int pos = 0)const; //從pos開始查詢字串s在當前串中的位置

**string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之間的部分替換為字串s

**string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之間的部分替換為s的前n個字元

**string &insert(int p0,const string &s, int pos, int n);//在p0位置插入字串s中pos開始的前n個字元

**iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字元,返回刪除後迭代器的位置

*string &insert(int p0, const char *s);

STL string的常用函式

string npos 這是string類中的乙個成員變數,一般應用在判斷系統查詢函式的返回值上,若等於該值,表明沒有符合查詢條件的結果值。find函式 在乙個字串中查詢指定的單個字元或字元組。如果找到,就返回首次匹配的開始位置 如果沒有找到匹配的內容,則返回string npos。一般有兩個輸入引...

STL String常用函式備註

對於string庫的函式用的比較少,常規的以下size 函式什麼的不在列舉,但是有些函式很好用,手寫做一下備註,省的到處查 1.string使用print輸出 常規條件下string只能使用cin和cout,但是使用printf輸出借助函式也可以 使用c str 函式 string str abcd...

STL string的關鍵函式

string find 1.如果string sub abc string s cdeabcigld s.find sub s.rfind sub 這兩個函式,如果完全匹配,才返回匹配的索引,即 當s中含有abc三個連續的字母時,才返回當前索引。s.find first of sub s.find ...