STL string的常用函式

2021-07-10 23:09:26 字數 4429 閱讀 4278

string::npos:這是string類中的乙個成員變數,一般應用在判斷系統查詢函式的返回值上,若等於該值,表明沒有符合查詢條件的結果值。

find函式:在乙個字串中查詢指定的單個字元或字元組。如果找到,就返回首次匹配的開始位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入引數,乙個是待查詢的字串,乙個是查詢的起始位置,預設起始位置為0.

find_first_of函式:在乙個字串中進行查詢,返回值是第乙個與指定字串中任何字元匹配的字元位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入引數,乙個是待查詢的字串,乙個是查詢的起始位置,預設起始位置為0.

find_last_of函式:在乙個字串中進行查詢,返回值是最後乙個與指定字串中任何字元匹配的字元位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入引數,

乙個是待查詢的字串,乙個是查詢的起始位置,預設起始位置為0.

find_first_not_of函式:在乙個字串中進行查詢,返回值是第乙個與指定字串中任何字元都不匹配的字元位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入引數,

乙個是待查詢的字串,乙個是查詢的起始位置,預設起始位置為0.

find_last_not_of函式:

在乙個字串中進行查詢,返回下標值最大的與指定字串中任何字元都不匹配的字元位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入引數,

乙個是待查詢的字串,乙個是查詢的起始位置,預設起始位置為0.

rfind函式:對乙個串從尾至頭查詢指定的單個字元或字元組,如果找到,就返回首次匹配的開始位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入引數,

乙個是待查詢的字串,乙個是查詢的起始位置,預設起始位置為0.

string類的替換函式:

string &replace(int p0, int n0,const char *s);//刪除從p0開始的n0個字元,然後在p0處插入串s

string &replace(int p0, int n0,const char *s, int n);//刪除p0開始的n0個字元,然後在p0處插入字串s的前n個字元

string &replace(int p0, int n0,const string &s);//刪除從p0開始的n0個字元,然後在p0處插入串s

string &replace(int p0, int n0,const string &s, int pos, int n);//刪除p0開始的n0個字元,然後在p0處插入串s中從pos開始的n個字元

string &replace(int p0, int n0,int n, char c);//刪除p0開始的n0個字元,然後在p0處插入n個字元c

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 &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之間的部分替換為串s

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

string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之間的部分替換成[first,last)之間的字串

string類的插入函式:

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

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

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

string &insert(int p0,const string &s, int pos, int n);

//前4個函式在p0位置插入字串s中pos開始的前n個字元

string &insert(int p0, int n, char c);//此函式在p0處插入n個字元c

iterator insert(iterator it, char c);//在it處插入字元c,返回插入後迭代器的位置

void insert(iterator it, const_iterator first, const_iterator last);//在it處插入[first,last)之間的字元

void insert(iterator it, int n, char c);//在it處插入n個字元c

string類的刪除函式

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

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

string &erase(int pos = 0, int n = npos);//刪除pos開始的n個字元,返回修改後的字串

string類的迭代器處理:

string類提供了向前和向後遍歷的迭代器iterator,迭代器提供了訪問各個字元的語法,類似於指標操作,迭代器不檢查範圍。

用string::iterator或string::const_iterator宣告迭代器變數,const_iterator不允許改變迭代的內容。常用迭代器函式有:

const_iterator begin()const;

iterator begin();                //返回string的起始位置

const_iterator end()const;

iterator end();                    //返回string的最後乙個字元後面的位置

const_iterator rbegin()const;

iterator rbegin();                //返回string的最後乙個字元的位置

const_iterator rend()const;

iterator rend();                    //返回string第乙個字元位置的前面

rbegin和rend用於從後向前的迭代訪問,通過設定迭代器string::reverse_iterator,string::const_reverse_iterator實現

字串流處理:

通過定義ostringstream和istringstream變數實現,標頭檔案中

例如:string input("hello,this is a test");

istringstream is(input);

string s1,s2,s3,s4;

is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"

ostringstream os;

os<

cout<

int main()

std::cout << "找到所有的a: \n";

offset = st.find("a", 0);

while (offset != std::string::npos)

std::cout << "裁剪字串:\n";

st.erase(10, 13);

std::cout << st << std::endl;

std::string::iterator it = find(st.begin(),st.end(),'g');

if (it != st.end())

std::cout << st.length() << std::endl;

std::cout << "input : \n";

std::string str;

getline(std::cin, str);//大小寫轉換

transform(str.begin(), str.end(),str.begin(), toupper); //轉大寫而且放回原來的地方

std::cout << str << std::endl;

std::cout << "反** \n";

reverse(st.begin(),st.end()); //反轉

std::cout << str << std::endl;

getchar();

return 0;

}

stl String常用函式

原文 string const char s 用c字串s初始化 const char c str const 返回乙個以null終止的c字串,用printf 輸出時需要 int size const 返回當前字串的大小 int length const 返回當前字串的長度 bool empty co...

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 ...