string 字符串在全部的語言中都很是重要,c++也不例外,接下來咱們將介紹string中的經常使用方法java
1. size() 和 length() 函數 : 他們返回字符串的真實長度,且不會由於空格而截斷,這兩個方法徹底等價,使用及輸出以下:ios
#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()函數和rfind()函數 : 這兩個函數用於查找字串在母串中的位置,而且返回該位置,固然若是找不到就會返回一個特別的標記string::nops,而find()函數是從字符串開始指針向後進行查找,rfind()函數是從字符串的結束指針開始向前查找,其使用及輸出以下:c++
#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; }
值得注意的是咱們能夠經過組合使用這兩個函數來實現判斷該子串是否惟一存在於母串中,其實現代碼以下:數組
#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()函數和find_first_of()函數:從函數名咱們也能夠知道find_last_of()函數是找這個子串在母串中最後一次出現的位置而且將該位置返回;而find_first_of()函數是找這個子串在母串中最後一次出現的位置並將該位置返回,其使用及輸出以下:app
#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()函數:該函數用於將目標串的值複製到該串上,而且只複製值,其使用及輸出以下:函數
#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()函數,把當前字符串清空,這時候若是調用string::size()函數或string::length()函數將返回0,其使用及輸出以下:spa
#include <iostream> #include <string> using namespace std; int main(void) { string s = "hello worldh"; s.clear(); cout<<"clear後的串的長度"<<s.size()<<endl; }
6.resize()函數,該函數能夠將字符串變長到指定長度,若小於本來字符串的長度,則會截斷原字符串;這個函數的一個重載形式是str.resize(length,'s') 能夠用該輸入字符's'來對字符串進行擴充至length的長度,該函數的使用及輸出以下:3d
#include <iostream> #include <string> using namespace std; int main(void) { string s = "hello worldh"; s.resize(5); // s會變爲 hello cout<<s<<endl; s.resize(10,'C'); // s 會變爲 helloCCCCC cout<<s<<endl; }
7. replace(pos,len,dist)函數: 該函數用於將該串從pos位置開始將長度爲len的字串替換爲dist串,值得注意的是該函數只替換一次,這與市面上的py和java等語言不同,須要留意,該函數的使用和輸出以下:
指針
#include <iostream> #include <string> using namespace std; int main(void) { string s = "hello worldh"; s.replace(s.find("h"),2,"#"); // 把從第一個h開始的兩個字符變爲一個字符 # cout<<"替換後的字符串爲: "<<s<<endl; }
那麼既然C++自己不提供,替換全部子串的函數,咱們就本身實現一個,其代碼以下:code
// 替換字符串裏的全部指定字符 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)函數:該函數刪除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; // 將會輸出lo worldh }
9.substr(index,length)函數:該函數從index開始截斷到長度爲length並返回截斷的子串;值得注意的是,該函數不改變母串的值,其使用及輸出以下:
#include <iostream> #include <string> using namespace std; int main(void) { s = s.substr(0,5); cout<<"截斷並賦值後的字符串爲:"<<s<<endl; // 會輸出hello }
10 . push_back(char c)函數,pop_back()函數,append(string s)函數:push_back(char c)函數往該字符串的尾端加入一個字符;pop_back()函數從該字符串的尾端彈出一個字符;而apend(string s)函數將會在該字符串的末尾添加一個字符串,而且返回添加後字符串的引用。他們的使用及輸出以下圖所示:
#include <iostream> #include <string> using namespace std; int main(void) { string s = "hello worldh"; // s.erase(s.find("h"),3); s.pop_back(); //彈出串的最後一個元素 cout<<"彈出串尾元素後的字符串爲: "<<s<<endl; s.push_back('s'); // 在串的最後添加一個字符 cout<<"往串尾添加字符後的字符串爲: "<<s<<endl; s.append("hhh"); // 在串的最後添加一個字符串 cout<<"往串尾添加字符串後的字符串爲: "<<s<<endl; }
以上就是string中比較重要的函數的所有內容了,既然咱們學完了該內容,那咱們接下來作一條題來熟悉一下這些函數中的一些吧(題目與代碼以下代碼塊,題目出自leetcode):
1 // 給你一份『詞彙表』(字符串數組) words 和一張『字母表』(字符串) chars。 2 // 假如你能夠用 chars 中的『字母』(字符)拼寫出 words 中的某個『單詞』(字符串),那麼咱們就認爲你掌握了這個單詞。 3 4 // 注意:每次拼寫時,chars 中的每一個字母都只能用一次。 5 // 返回詞彙表 words 中你掌握的全部單詞的 長度之和。 6 7 // 輸入:words = ["cat","bt","hat","tree"], chars = "atach" 8 // 輸出:6 9 // 解釋: 10 // 能夠造成字符串 "cat" 和 "hat",因此答案是 3 + 3 = 6。 11 12 // 輸入:words = ["hello","world","leetcode"], chars = "welldonehoneyr" 13 // 輸出:10 14 // 解釋: 15 // 能夠造成字符串 "hello" 和 "world",因此答案是 5 + 5 = 10。 16 17 #include <iostream> 18 #include <vector> 19 #include <string> 20 using namespace std; 21 class Solution 22 { 23 public: 24 int countCharacters(vector<string> &words, string chars) 25 { 26 int count = 0; 27 bool flag = false; // 標記 28 string c_chars(chars); // 構造c_chars保存chars 29 for (int i = 0; i < words.size(); i++) // 迭代單詞表 30 { 31 if (c_chars.size() < words[i].size()) //若是單詞的字母多於可選字母,則跳過這個單詞 32 continue; 33 for (int j = 0; j < words[i].size(); j++) // 迭代可選擇的字母 34 { 35 int index = c_chars.find(words[i][j]); 36 if (index != c_chars.npos) // 能找到這個字母 37 { 38 flag = true; 39 c_chars.erase(index, 1); // 從c_chars()刪除這個字母 40 } 41 else 42 { 43 flag = false; // 不能找到,意味着不能組成這個單詞 44 break; //跳出此次循環 45 } 46 } 47 if (flag) // 若是符合則計數加1 48 count += words[i].size(); 49 c_chars.assign(chars); // 把chars的值在再次賦值給c_chars 50 } 51 return count; 52 } 53 };
最後感謝你們的閱讀,string中這些的函數組合起來能夠說是威力無窮,因此仍是要好好掌握的。
以上就是此次隨筆的所有內容了;其中有小部份內容參考了一些其餘文章;好了,咱們下次見,謝謝你們。