c 中string的用法

2021-08-31 23:49:22 字數 1295 閱讀 4279

#include#include"string"

#include"algorithm"

using namespace std;

//string的賦值

void f1()

//string的遍歷

void f2()

cout << endl;

//2:迭代器

for(string::iterator it = s1.begin(); it != s1.end(); it++)

cout << endl;

//3 異常處理

try}

catch(...)

try}

catch(...)

}//字元指標和sting的轉換

void f4()

; s1.copy(buf, 3, 1); //拷貝3個字元,從1個字元開始(位置下標從0開始) //注:不會自動加上字串結束標誌

cout << "buf: " << buf << endl;

}//字串的連線

void f5()

//字串的查詢和替換

void f6()

//把所有的wbm換成大寫

offindex = s1.find("wbm", 0);

while (offindex != string::npos) //不等於-1

cout << endl << "s1替換後的結果為: " << s1 << endl;

//把aaa替換成大寫

string s2 = "aaa bbb ccc";

s2.replace(0, 3, "aaa"); //從第0個位置開始替換3個

cout << endl << "s2: " << s2 << endl;

}//區間刪除和插入

void f7()

cout << "s1刪除l以後的結果為:" << s1 << endl;

s1.erase(s1.begin(), s1.end());

cout << "s1全部刪除:" << s1 << endl;

cout << "s1的長度為: " << s1.length() << endl;

//插入

string s2 = "bbb";

s2.insert(0, "aaa");

cout << "s2: " << s2 << endl;

}//大小寫轉換

void f8()

int main()

c 中的string用法

向string 的後面加字元或字串。比 push back 更靈活 1 向string 的後面加c string basic type ptr string s hello s hello const char c out there s hello out there 2 向string 的後面加...

c 中string的用法

c 中string的用法 string在c 中是作什麼用的阿?既有string str宣告變數的,也有string n,的,我是新手,謝謝了。之所以拋棄char 的字串而選用c 標準程式庫中的string類,是因為他和前者比較起來,不必 擔心記憶體是否足夠 字串長度等等,而且作為乙個類出現,他整合的...

C 中string的用法

之所以拋棄char 的字串而選用c 標準程式庫中的string類,是因為他和前者比較起來,不必 擔心記憶體是否足夠 字串長度等等,而且作為乙個類出現,他整合的操作函式足以完成我們大多數情況下 甚至是100 的需要。我們可以用 進行賦值操作,進行比較,做串聯 是不是很簡單?我們盡可以把它看成是c 的基...