C++中 string 中的常用方法使用心得
string 字符串在所有的語言中都非常重要,c++也不例外,接下來我們將介紹string中的常用方法
1. size() 和 length() 函數(shù) : 他們返回字符串的真實(shí)長度,且不會(huì)因?yàn)榭崭穸財(cái)?,這兩個(gè)方法完全等價(jià),使用及輸出如下:
#include<iostream> #include<string> using namespace std; int main(void) { string s = "dasddasd"; printf("size()返回的長度為:%lu\nlength()返回的長度為:%lu",s.size(),s.length()); return 0; }
2. find()函數(shù)和rfind()函數(shù) : 這兩個(gè)函數(shù)用于查找字串在母串中的位置,并且返回該位置,當(dāng)然如果找不到就會(huì)返回一個(gè)特別的標(biāo)記string::nops
,而find()函數(shù)是從字符串開始指針向后進(jìn)行查找,rfind()函數(shù)是從字符串的結(jié)束指針開始向前查找,其使用及輸出如下:
#include<iostream> #include<string> using namespace std; int main(void) { string s = "hello worldh"; int index = s.find("h"); // 從串首向后查找 int index2 = s.find("h",2) // 固定位置后子串在母串的位置 int index1 = s.rfind("h"); // 從串尾向前查找 printf("(find()):字母h在母串中的位置為:%d\n", index); printf("(rfind()):字母h在母串中的位置為:%d", index1); return 0; }
值得注意的是我們可以通過組合使用這兩個(gè)函數(shù)來實(shí)現(xiàn)判斷該子串是否唯一存在于母串中,其實(shí)現(xiàn)代碼如下:
#include<iostream> #include<string> using namespace std; inline bool whetherOnly(string &str,string &base){ return base.find(str) == base.rfind(str); }
3. find_last_of()
函數(shù)和find_first_of()
函數(shù):從函數(shù)名我們也可以知道find_last_of()
函數(shù)是找這個(gè)子串在母串中最后一次出現(xiàn)的位置并且將該位置返回;而find_first_of()
函數(shù)是找這個(gè)子串在母串中最后一次出現(xiàn)的位置并將該位置返回,其使用及輸出如下:
#include <iostream> #include <string> using namespace std; int main(void) { string s = "hello worldh"; int index = s.find_first_of("h"); int index1 = s.find_last_of("h"); printf("(find_first_of()):字母h在母串中的位置為:%d\n", index); printf("(find_last_of()):字母h在母串中的位置為:%d", index1); }
4.assign()函數(shù):該函數(shù)用于將目標(biāo)串的值復(fù)制到該串上,并且只復(fù)制值,其使用及輸出如下:
#include <iostream> #include <string> using namespace std; int main(void) { string s = "hello worldh"; s.clear(); s.assign("hello world"); cout<<s<<endl; }
5.clear()函數(shù),把當(dāng)前字符串清空,這時(shí)候如果調(diào)用string::size()
函數(shù)或string::length()
函數(shù)將返回0,其使用及輸出如下:
#include <iostream> #include <string> using namespace std; int main(void) { string s = "hello worldh"; s.clear(); cout<<"clear后的串的長度"<<s.size()<<endl; }
6.resize()函數(shù),該函數(shù)可以將字符串變長到指定長度,若小于原本字符串的長度,則會(huì)截?cái)嘣址?;這個(gè)函數(shù)的一個(gè)重載形式是str.resize(length,'s')
可以用該輸入字符's'來對(duì)字符串進(jìn)行擴(kuò)充至length的長度,該函數(shù)的使用及輸出如下:
#include <iostream> #include <string> using namespace std; int main(void) { string s = "hello worldh"; s.resize(5); // s會(huì)變?yōu)?hello cout<<s<<endl; s.resize(10,'C'); // s 會(huì)變?yōu)?helloCCCCC cout<<s<<endl; }
7. replace(pos,len,dist)
函數(shù): 該函數(shù)用于將該串從pos位置開始將長度為len的字串替換為dist串,值得注意的是該函數(shù)只替換一次,這與市面上的py和java等語言不一樣,需要留意,該函數(shù)的使用和輸出如下:
#include <iostream> #include <string> using namespace std; int main(void) { string s = "hello worldh"; s.replace(s.find("h"),2,"#"); // 把從第一個(gè)h開始的兩個(gè)字符變?yōu)橐粋€(gè)字符 # cout<<"替換后的字符串為: "<<s<<endl; }
那么既然C++本身不提供,替換所有子串的函數(shù),我們就自己實(shí)現(xiàn)一個(gè),其代碼如下:
// 替換字符串里的所有指定字符 string replace(string &base, string src, string dst) //base為原字符串,src為被替換的子串,dst為新的子串 { int pos = 0, srclen = src.size(), dstlen = dst.size(); while ((pos = base.find(src, pos)) != string::npos) { base.replace(pos, srclen, dst); pos += dstlen; } return base; }
8. erase(index,length)
函數(shù):該函數(shù)刪除index位置后length長度的子串,其代碼及輸出如下:
#include <iostream> #include <string> using namespace std; int main(void) { string s = "hello worldh"; s.erase(s.find("h"),3); cout<<"擦除過后的串"<<s<<endl; // 將會(huì)輸出lo worldh }
9.substr(index,length)函數(shù):該函數(shù)從index開始截?cái)嗟介L度為length并返回截?cái)嗟淖哟?;值得注意的是,該函?shù)不改變母串的值,其使用及輸出如下:
#include <iostream> #include <string> using namespace std; int main(void) { s = s.substr(0,5); cout<<"截?cái)嗖①x值后的字符串為:"<<s<<endl; // 會(huì)輸出hello }
10 . push_back(char c)函數(shù),pop_back()函數(shù),append(string s)函數(shù):push_back(char c)函數(shù)往該字符串的尾端加入一個(gè)字符;pop_back()函數(shù)從該字符串的尾端彈出一個(gè)字符;而apend(string s)函數(shù)將會(huì)在該字符串的末尾添加一個(gè)字符串,并且返回添加后字符串的引用。他們的使用及輸出如下圖所示:
#include <iostream> #include <string> using namespace std; int main(void) { string s = "hello worldh"; // s.erase(s.find("h"),3); s.pop_back(); //彈出串的最后一個(gè)元素 cout<<"彈出串尾元素后的字符串為: "<<s<<endl; s.push_back('s'); // 在串的最后添加一個(gè)字符 cout<<"往串尾添加字符后的字符串為: "<<s<<endl; s.append("hhh"); // 在串的最后添加一個(gè)字符串 cout<<"往串尾添加字符串后的字符串為: "<<s<<endl; }
以上就是string中比較重要的函數(shù)的全部內(nèi)容了,既然我們學(xué)完了該內(nèi)容,那我們接下來做一條題來熟悉一下這些函數(shù)中的一些吧(題目與代碼如下代碼塊,題目出自leetcode):
// 給你一份『詞匯表』(字符串?dāng)?shù)組) words 和一張『字母表』(字符串) chars。 // 假如你可以用 chars 中的『字母』(字符)拼寫出 words 中的某個(gè)『單詞』(字符串),那么我們就認(rèn)為你掌握了這個(gè)單詞。 // 注意:每次拼寫時(shí),chars 中的每個(gè)字母都只能用一次。 // 返回詞匯表 words 中你掌握的所有單詞的 長度之和。 // 輸入:words = ["cat","bt","hat","tree"], chars = "atach" // 輸出:6 // 解釋: // 可以形成字符串 "cat" 和 "hat",所以答案是 3 + 3 = 6。 // 輸入:words = ["hello","world","leetcode"], chars = "welldonehoneyr" // 輸出:10 // 解釋: // 可以形成字符串 "hello" 和 "world",所以答案是 5 + 5 = 10。 #include <iostream> #include <vector> #include <string> using namespace std; class Solution { public: int countCharacters(vector<string> &words, string chars) { int count = 0; bool flag = false; // 標(biāo)記 string c_chars(chars); // 構(gòu)造c_chars保存chars for (int i = 0; i < words.size(); i++) // 迭代單詞表 { if (c_chars.size() < words[i].size()) //如果單詞的字母多于可選字母,則跳過這個(gè)單詞 continue; for (int j = 0; j < words[i].size(); j++) // 迭代可選擇的字母 { int index = c_chars.find(words[i][j]); if (index != c_chars.npos) // 能找到這個(gè)字母 { flag = true; c_chars.erase(index, 1); // 從c_chars()刪除這個(gè)字母 } else { flag = false; // 不能找到,意味著不能組成這個(gè)單詞 break; //跳出這次循環(huán) } } if (flag) // 如果符合則計(jì)數(shù)加1 count += words[i].size(); c_chars.assign(chars); // 把chars的值在再次賦值給c_chars } return count; } };
最后感謝大家的閱讀,string中這些的函數(shù)組合起來可以說是威力無窮,所以還是要好好掌握的。
總結(jié)
到此這篇關(guān)于C++中 string 中的方法的使用詳解(心得)的文章就介紹到這了,更多相關(guān)C++ string方法使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言實(shí)現(xiàn)經(jīng)典24點(diǎn)紙牌益智游戲
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)經(jīng)典24點(diǎn)紙牌益智游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10C語言實(shí)現(xiàn)括號(hào)配對(duì)的方法示例
本文主要介紹了C語言實(shí)現(xiàn)括號(hào)配對(duì)的方法示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09VS2010 boost標(biāo)準(zhǔn)庫開發(fā)環(huán)境安裝教程
這篇文章主要為大家詳細(xì)介紹了VS2010 boost標(biāo)準(zhǔn)庫開發(fā)環(huán)境的安裝教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04使用mmap實(shí)現(xiàn)多進(jìn)程對(duì)大文件拷貝
這篇文章主要介紹了使用mmap實(shí)現(xiàn)多進(jìn)程對(duì)大文件拷貝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10C++文件關(guān)鍵詞快速定位出現(xiàn)的行號(hào)實(shí)現(xiàn)高效搜索
這篇文章主要為大家介紹了C++文件關(guān)鍵詞快速定位出現(xiàn)的行號(hào)實(shí)現(xiàn)高效搜索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10C語言實(shí)現(xiàn)的學(xué)生選課系統(tǒng)代碼分享
這篇文章主要介紹了C語言實(shí)現(xiàn)的學(xué)生選課系統(tǒng)代碼分享,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10