#include <iostream> // std::cout#include <algorithm> // std::find#include <vector> // std::vectorusing namespace std;int main() { //find() 函數作用於普通數組 char stl[] ="http://c.biancheng.net/stl/"; //調用 find() 查找第一個字符 'c' char * p = find(stl, stl + strlen(stl), 'c'); //判斷是否查找成功 if (p != stl + strlen(stl)) { cout << p << endl; } //find() 函數作用於容器 std::vector<int> myvector{ 10,20,30,40,50 }; std::vector<int>::iterator it;
it = find(myvector.begin(), myvector.end(), 30); if (it != myvector.end()) cout << "查找成功:" << *it; else cout << "查找失敗"; return 0;}對於 find() 函數的底層實現,C++ 標準庫中給出了參數代碼,感興趣的讀者可自行研究:
template<class InputIterator, class T>InputIterator find (InputIterator first, InputIterator last, const T& val){ while (first!=last) { if (*first==val) return first; ++first; } return last;}查找第一次出現的目標字符串:
#include<iostream>#include<cstdio>using namespace std;
int main(){string s1 = "abcdef";string s2 = "de";int ans = s1.find(s2) ; //在S1中查找子串S2cout<<ans<<endl;system("pause");}說明:如果查找成功則輸出查找到的第一個位置,否則返回-1;
查找從指定位置開始的第一次出現的目標字符串:
#include<iostream>#include<csdtio>using namespace std;
int main(){string s1 = "abcdef";string s2 = "de";int ans = s1.find(s2, 2) ; //從S1的第二個字符開始查找子串S2cout<<ans<<endl;system("pause");}find_first_of()表示查找字符串的某個字符最先出現的位置,find_first_of()不是全匹配,即它不是必須要查找的字符串在被查找的字符串中全部出現,而是出現個別字符即可。(我覺得這是個神馬需求?沒用過!!!)
find_last_of()函數與find_first_of()功能差不多,只不過find_first_of()是從字符串的前面往後面搜索,而find_last_of()是從字符串的後面往前面搜索。
rfind()函數是反向查找字符串,即找到最後一個與子串匹配的位置。
find_first_not_of()函數是找到第一個不與子串匹配的位置。(這個也沒有用到過)
2、C++ substr()
substr()是C++語言函數,主要功能是複製子字符串,要求從指定位置開始,並具有指定的長度。如果沒有指定長度_Count或_Count+_Off超出了源字符串的長度,則子字符串將延續到源字符串的結尾。——摘自百科詞條
語法:
substr(size_type _Off = 0,size_type _Count = npos)一種構造string的方法
形式 :s.substr(pos, len)
返回值:string,包含s中從pos開始的len個字符的拷貝(pos的默認值是0,len的默認值是s.size() - pos,即不加參數會默認拷貝整個s)
異常 :若pos的值超過了string的大小,則substr函數會拋出一個out_of_range異常;若pos+n的值超過了string的大小,則substr會調整n的值,只拷貝到string的末尾
#include<iostream>#include<string>using namespace std;int main(){ string s="sfsa"; string a=s.substr(0,3); string b=s.substr(); string c=s.substr(2,3); cout<<a<<endl; cout<<b<<endl; cout<<c<<endl; return 0;}3、C++ replace() (這變態,重載了9個,給我挑的上氣不接下氣,不管他重載多少我們快速找出最適合自己那一款就好)
用法一:用str替換指定字符串從起始位置pos開始長度為len的字符
用法二: 用str替換 迭代器起始位置 和 結束位置 的字符
用法三: 用substr的指定子串(給定起始位置和長度)替換從指定位置上的字符串
用法四:string轉char*時編譯器可能會報出警告,不建議這樣做
用法五:string轉char*時編譯器可能會報出警告,不建議這樣做
用法六:string轉char*時編譯器可能會報出警告,不建議這樣做
用法七:string轉char*時編譯器可能會報出警告,不建議這樣做
用法八: 用重複n次的c字符替換從指定位置pos長度為len的內容
用法九: 用重複n次的c字符替換從指定迭代器位置(從i1開始到結束)的內容
這九個介紹往這裡一列給人看的腦瓜子嗡嗡的!
上例子!!!(這邊我寫3個,其他的都是大同小異的東西,一通百通;用的時候稍微Baidu一下瞬間會了,平時經常不用也記不住)
用法一:用str替換指定字符串從起始位置pos開始長度為len的字符
string& replace (size_t pos, size_t len, const string& str);
#include<iostream>#include<string>using namespace std;int main(){string str = "he is@ a@ good boy";str=str.replace(str.find("a"),2,"#"); //從第一個a位置開始的兩個字符替換成#cout<<str<<endl; return 0;}用法二: 用str替換 迭代器起始位置 和 結束位置 的字符string& replace (const_iterator i1, const_iterator i2, const string& str);
#include<iostream>#include<string>using namespace std;int main(){string str = "he is@ a@ good boy"; str=str.replace(str.begin(),str.begin()+5,"#"); //用#替換從begin位置開始的5個字符 cout<<str<<endl; return 0; }用法三: 用substr的指定子串(給定起始位置和長度)替換從指定位置上的字符串
string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
#include<iostream>#include<string>using namespace std;int main(){string str = "he is@ a@ good boy"; str=str.replace(str.begin(),str.begin()+5,"#"); //用#替換從begin位置開始的5個字符 cout<<str<<endl; return 0; }4、C++中string::npos的一些用法總結
npos是一個常數,表示size_t的最大值(Maximum value for size_t)。許多容器都提供這個東西,用來表示不存在的位置,類型一般是std::container_type::size_type。
#include <iostream> #include <limits> #include <string> using namespace std;
int main() { size_t npos = -1; cout << "npos: " << npos << endl; cout << "size_t max: " << numeric_limits<size_t>::max() << endl;}執行結果為:
npos: 4294967295
size_t max: 4294967295
可見他們是相等的,也就是說npos表示size_t的最大值
npos可以表示string的結束位置,是string::type_size 類型的,也就是find()返回的類型。find函數在找不到指定值的情況下會返回string::npos。舉例如下(計算字符串中含有的不同字符的個數):
#include <iostream>#include <string>using namespace std;int main(){ string b; getline(cin,b); int count=0; for(int i=0;i<=127;i++) if(b.find(i)!=string::npos) count++; cout<<count;}string::npos作為string的成員函數的一個長度參數時,表示「直到字符串結束(until the end of the string)」。例如:
tmpname.replace(idx+1, string::npos, suffix);這裡的string::npos就是一個長度參數,表示直到字符串的結束,配合idx+1表示,string的剩餘部分。
#include <iostream> #include <limits> #include <string> using namespace std; int main() { string filename = "test.cpp"; cout << "filename : " << filename << endl;
size_t idx = filename.find('.'); //as a return value if(idx == string::npos) { cout << "filename does not contain any period!" << endl; } else { string tmpname = filename; tmpname.replace(idx + 1, string::npos, "xxx"); //string::npos作為長度參數,表示直到字符串結束 cout << "repalce: " << tmpname << endl; } }執行結果如下:
filename:test.cpp
replace: test.xxx
值得注意的地方:
int idx = str.find("abc");if (idx == string::npos) ...上述代碼中,idx的類型被定義為int,這是錯誤的,即使定義為 unsigned int 也是錯的,它必須定義為 string::size_type。因為 string::size_type (由字符串配置器 allocator 定義) 描述的是 size,故需為無符號整數型別。因為預設配置器以型別 size_t 作為 size_type,於是 -1 被轉換為無符號整數型別,npos 也就成了該型別的最大無符號值。不過實際數值還是取決於型別 size_type 的實際定義。不幸的是這些最大值都不相同。事實上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是兩者型別大小不同)。因此,比較式 idx == string::npos 中,如果 idx 的值為-1,由於 idx 和字符串string::npos 型別不同,比較結果可能得到 false。
要想判斷 find() 的結果是否為npos,最好的辦法是直接比較:
if (str.find("abc") == string::npos) { ... }寫在最後的一句話:
萬丈高樓平地起,再普通的改變也能改變普通。