string所有函数详解——史上最全,最贴心
簡介
區(qū)別于#include<string.h>頭文件,#include<string>頭文件中的函數(shù)專門用于處理C++中string字符串。而前者是用來處理C語言中char數(shù)組的頭文件。
常用函數(shù)
一、運算符: +、=、[]
1、在C++中,string重載了+、=、和[],可以讓我們十分方便的對字符串進行運算。
#include<iostream> #include<string> using namespace std; int main() {string s1 = "123", s2 = "567";string s3 = s1 + s2;cout << s3 << endl; //實現(xiàn)等號賦值, 加號串聯(lián)cout << s3[0] << endl; //實現(xiàn)對字符串中某個值的訪問return 0; }輸出:
123567
1
二、求長度:length()、size()
二者都可以用來求長度。length()為獲取字符串長度。 size()為獲取字符串這個容器中元素的個數(shù)
#include<iostream> #include<string> using namespace std; int main() {string s1 = "123";cout << s1.length() << endl;cout << s1.size() << endl;return 0; }輸出:
3
3
三、翻轉(zhuǎn)字符串:reverse()
它本是#include<algorithm>頭文件中的函數(shù), 但在字符串中應(yīng)用非常廣泛,因此我選擇把它加入string頭文件中講解。
1、實現(xiàn)對全部字符串的翻轉(zhuǎn)。
#include<iostream> #include<algorithm> #include<string> using namespace std; int main() {string s1 = "12345";reverse(s1.begin(), s1.end());cout << s1 << endl;return 0; }輸出:
54321
2、通過使用迭代器, 還可以實現(xiàn)對指定位置的翻轉(zhuǎn)。
#include<iostream> #include<string> #include<algorithm> using namespace std; int main() {string s1 = "12345";string::iterator it = s1.begin();reverse(it, it+3);cout << s1 << endl;return 0; }輸出:
32145
四、判斷字符串是否為空:empty()
如果為空,則值為1,反之為0
#include<iostream> #include<string> using namespace std; int main() {string s1 = "12345";if(s1.empty() == 1) cout << "字符串為空";else cout << "字符串非空";return 0; }輸出:
字符串非空
五、字符串清空函數(shù):clear()
將字符串清空
#include<iostream> #include<string> using namespace std; int main() {string s1 = "12345";s1.clear();if(s1.empty() == 1) cout << "字符串為空";else cout << "字符串非空";return 0; }輸出:
字符串為空
六、刪除字符串:erase()
用法為:erase(int x, int num),從x位置向后刪除(不包括x),刪除num個字符。
#include<iostream> #include<string> using namespace std; int main() {string s1 = "12345";s1.erase(0, 2); //從0的位置向后刪,刪除2個cout << s1; return 0; }輸出:
345
七、插入函數(shù):insert()
用法為:insert(int x1, int num, char c),從x位置向后插入(不包括x),插入num個字符c。
#include<iostream> #include<string> using namespace std; int main() {string s1 = "12345";s1.insert(0, 1, '1');cout << s1; return 0; }輸出:
112345
八、查找函數(shù)之find()、rfind()、find_first_of()、find_first_not_of()、 find_last_of()、find_last_not_of()
8.1 find()函數(shù)
8.1.1、查找指定字符, 從某個位置向后開始查找, 返回該位置,若查找不到,則返回-1
#include<iostream> #include<string> using namespace std; int main() {string s1 = "123145";int find1 = s1.find('1', 1); cout << find1 << endl; int find2 = s1.find('6');cout << find2;return 0; }輸出:
3
-1
8.1.2、查找字符串, 原理同上, 若查找到返回字符串的起始位置(注意是起始位置)。
#include<iostream> #include<string> using namespace std; int main() {string s1 = "123145";int find1 = s1.find("23", 0); cout << find1 << endl; int find2 = s1.find("56", 0);cout << find2;return 0; }輸出:
1
-1
8.2 rfind()函數(shù)
從后往前查找指定的字符或字符串,用法同上。
8.3 find_first_of()函數(shù)
查找第一個等于x字符的位置。 用法同上
8.4 find_first_not_of()函數(shù)
查找第一個不等于x字符的位置。 用法同上
8.5 find_last_of()函數(shù)
查找最后一個等于x字符的位置(注意是查找最后一個,而不是從后往前查找)。
8.6 find_last_not_of()函數(shù)
查找最后一個不等于x字符的位置。 用法同上
注意:只有find和rfind函數(shù)可以查找字符串!其他函數(shù)查找會出現(xiàn)值不準確的情況!
九、替換函數(shù):replace()
把0后的兩個字符替換成A
#include<iostream> #include<string> using namespace std; int main() {string s1 = "12345";s1.replace(0, 2, "A");cout << s1;return 0; }輸出:
A345
十、字符串轉(zhuǎn)換類函數(shù)
10.1 將string轉(zhuǎn)換成char*的函數(shù): c_str()
在做題過程中, printf往往比cout要靈活一些,尤其是在輸出指定內(nèi)容時, 但我們知道,printf無法輸出string型字符串,我們就可以用c_str()函數(shù)進行轉(zhuǎn)化后輸出。
#include<iostream> #include<string> using namespace std; int main() {string s1 = "12345";printf("%s", s1.c_str());return 0; }輸出:
12345
10.2 將其他類型變量轉(zhuǎn)換成string字符串:to_string()
#include<iostream> #include<string> using namespace std; int main() {double pi = 3.1415926;string s1 = to_string(pi);cout << s1;return 0; }輸出:
3.1415926
十一、佛祖保佑,永無BUG泛濫
/* _ooOoo_o8888888o88" . "88(| -_- |)O\ = /O____/`---'\____. ' \\| |// `./ \\||| : |||// \/ _||||| -:- |||||- \| | \\\ - /// | || \_| ''\---/'' | |\ .-\__ `-` ___/-. /___`. .' /--.--\ `. . __."" '< `.___\_<|>_/___.' >'"".| | : `- \`.;`\ _ /`;.`/ - ` : | |\ \ `-. \_ __\ /__ _/ .-` / /======`-.____`-.___\_____/___.-`____.-'======`=---='.............................................佛祖鎮(zhèn)樓 BUG辟易佛曰:寫字樓里寫字間,寫字間里程序員;程序人員寫程序,又拿程序換酒錢。酒醒只在網(wǎng)上坐,酒醉還來網(wǎng)下眠;酒醉酒醒日復(fù)日,網(wǎng)上網(wǎng)下年復(fù)年。但愿老死電腦間,不愿鞠躬老板前;奔馳寶馬貴者趣,公交自行程序員。別人笑我忒瘋癲,我笑自己命太賤;不見滿街漂亮妹,哪個歸得程序員?*/ /* _ooOoo_ o8888888o 88" . "88 (| -_- |)O\ = /O ___/`---'\____ . ' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . __ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / / ======`-.____`-.___\_____/___.-`____.-'====== `=---='.............................................佛曰:bug泛濫,我已癱瘓!*/如果哪里有困惑,歡迎給筆者留言。
如果這篇博文對你產(chǎn)生了幫助,可以留下小小的一個贊哦,大家的支持是我更新的最大動力~
總結(jié)
以上是生活随笔為你收集整理的string所有函数详解——史上最全,最贴心的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【简便解法】1090 危险品装箱 (25
- 下一篇: 【简便解法】1091 N-自守数 (15