歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++String中用於查找的find系列函數淺析

C++String中用於查找的find系列函數淺析

日期:2017/3/1 9:29:41   编辑:Linux編程

總述:
以下所講的所有的string查找函數,都有唯一的返回類型,那就是size_type,即一個無符號整數(按打印出來的算)。若查找成功,返回按查找規則找到的第一個字符或子串的位置;若查找失敗,返回npos,即-1(打印出來為4294967295)。

1.fine()

原型:

//string (1)
size_type find (const basic_string& str, size_type pos = 0) const noexcept;
//c-string (2)
size_type find (const charT* s, size_type pos = 0) const;
//buffer (3)
size_type find (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type find (charT c, size_type pos = 0) const noexcept;

示例:

#include<iostream>
#include<string>

using namespace std;

int main()
{
//測試size_type find (charT c, size_type pos = 0) const noexcept;
string st1("babbabab");
cout << st1.find('a') << endl;//1 由原型知,若省略第2個參數,則默認從位置0(即第1個字符)起開始查找
cout << st1.find('a', 0) << endl;//1
cout << st1.find('a', 1) << endl;//1
cout << st1.find('a', 2) << endl;//4 在st1中,從位置2(b,包括位置2)開始,查找字符a,返回首次匹配的位置,若匹配失敗,返回npos
cout << st1.rfind('a',7) << endl;//6 關於rfind,後面講述
cout << st1.find('c', 0) << endl;//4294967295
cout << (st1.find('c', 0) == -1) << endl;//1
cout << (st1.find('c', 0) == 4294967295) << endl;//1 兩句均輸出1,原因是計算機中-1和4294967295都表示為32個1(二進制)
cout << st1.find('a', 100) << endl;//4294967295 當查找的起始位置超出字符串長度時,按查找失敗處理,返回npos
//測試size_type find (const basic_string& str, size_type pos = 0) const noexcept;
string st2("aabcbcabcbabcc");
string str1("abc");
cout << st2.find(str1, 2) << endl;//6 從st2的位置2(b)開始匹配,返回第一次成功匹配時匹配的串(abc)的首字符在st2中的位置,失敗返回npos
//測試size_type find (const charT* s, size_type pos = 0) const;
cout << st2.find("abc", 2) << endl; //6 同上,只不過參數不是string而是char*
//測試size_type find (const charT* s, size_type pos, size_type n) const;
cout << st2.find("abcdefg", 2, 3) << endl;//6 取abcdefg得前3個字符(abc)參與匹配,相當於st2.find("abc", 2)
cout << st2.find("abcbc", 0, 5) << endl;//1 相當於st2.find("abcbc", 0)
cout << st2.find("abcbc", 0, 6) << endl;//4294967295 第3個參數超出第1個參數的長度時,返回npos
return 0;
}

應用舉例:

//找出字符串str中所有的"abc"(輸出位置),若未找到,輸出"not find!"
#include<iostream>
#include<string>

using namespace std;

int main()
{
string str("babccbabcaabcccbabccabcabcabbabcc");
int num = 0;
size_t fi = str.find("abc", 0);
while (fi!=str.npos)
{
cout << fi << " ";
num++;
fi = str.find("abc", fi + 1);
}
if (0 == num)
cout << "not find!";
cout << endl;
return 0;
}
//運行結果:
//1 6 10 16 20 23 29

2.rfind()

原型:

//string (1)
size_type rfind (const basic_string& str, size_type pos = npos) const noexcept;
//c-string (2)
size_type rfind (const charT* s, size_type pos = npos) const;
//buffer (3)
size_type rfind (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type rfind (charT c, size_type pos = npos) const noexcept;

說明:
rfind()與find()很相似,差別在於查找順序不一樣,rfind()是從指定位置起向前查找,直到串首。例如,上例中的st1.rfind('a',7)一句,就是從st1的位置7(st1的最後一個字符b)開始查找字符a,第一次找到的是倒數第2個字符a,所以返回6。
關於rfind(),不再詳述,讀者可根據find()的示例,自行寫代碼學習rfind()。

3.find_first_of()

原型:

//string (1)
size_type find_first_of (const basic_string& str, size_type pos = 0) const noexcept;
//c-string (2)
size_type find_first_of (const charT* s, size_type pos = 0) const;
//buffer (3)
size_type find_first_of (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type find_first_of (charT c, size_type pos = 0) const noexcept;

說明:
在源串中從位置pos起往後查找,只要在源串中遇到一個字符,該字符與目標串中任意一個字符相同,就停止查找,返回該字符在源串中的位置;若匹配失敗,返回npos。

示例(僅給出部分原型的實例,對於其余原型,相信讀者有能力仿照前邊關於find()的實例來自行寫代碼測試和學習):

#include<iostream>
#include<string>

using namespace std;

int main()
{
//測試size_type find_first_of (charT c, size_type pos = 0) const noexcept;
string str("babccbabcc");
cout << str.find('a', 0) << endl;//1
cout << str.find_first_of('a', 0) << endl;//1 str.find_first_of('a', 0)與str.find('a', 0)
//測試size_type find_first_of (const basic_string& str, size_type pos = 0) const noexcept;
string str1("bcgjhikl");
string str2("kghlj");
cout << str1.find_first_of(str2, 0) << endl;//從str1的第0個字符b開始找,b不與str2中的任意字符匹配;再找c,c不與str2中的任意字符匹配;再找g,
//g與str2中的g匹配,於是停止查找,返回g在str1中的位置2
//測試size_type find_first_of (const charT* s, size_type pos, size_type n) const;
cout << str1.find_first_of("kghlj", 0, 20);//2 盡管第3個參數超出了kghlj的長度,但仍能得到正確的結果,可以認為,str1是和"kghlj+亂碼"做匹配
return 0;
}

應用舉例:

//將字符串中所有的元音字母換成*
//代碼來自C++ Reference,地址:http://www.cplusplus.com/reference/string/basic_string/find_first_of/
#include<iostream>
#include<string>

using namespace std;

int main()
{
std::string str("PLease, replace the vowels in this sentence by asterisks.");
std::string::size_type found = str.find_first_of("aeiou");
while (found != std::string::npos)
{
str[found] = '*';
found = str.find_first_of("aeiou", found + 1);
}
std::cout << str << '\n';
return 0;
}
//運行結果:
//PL**s* r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks

4.find_last_of()

原型:

//string (1)
size_type find_last_of (const basic_string& str, size_type pos = npos) const noexcept;
//c-string (2)
size_type find_last_of (const charT* s, size_type pos = npos) const;
//buffer (3)
size_type find_last_of (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type find_last_of (charT c, size_type pos = npos) const noexcept;

說明:
該函數與find_first_of()函數相似,只不過查找順序是從指定位置向前,這裡僅簡單舉例,不再贅述,讀者可參考find_first_of()自行學習。

示例:

#include<iostream>
#include<string>

using namespace std;

int main()
{
//測試size_type find_last_of (const charT* s, size_type pos = npos) const;
//目標串中僅有字符c與源串中的兩個c匹配,其余字符均不匹配
string str("abcdecg");
cout << str.find_last_of("hjlywkcipn", 6) << endl;//5 從str的位置6(g)開始想前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5
cout << str.find_last_of("hjlywkcipn", 4) << endl;//2 從str的位置4(e)開始想前找,e不匹配,再找d,d不匹配,再找c,c匹配,停止查找,
// 返回c在str中的位置5
cout << str.find_last_of("hjlywkcipn", 200) << endl;//5 當第2個參數超出源串的長度(這裡str長度是7)時,不會出錯,相當於從源串的最後一
// 個字符起開始查找
return 0;
}

5.find_first_not_of()
原型:

//string (1)
size_type find_first_not_of (const basic_string& str, size_type pos = 0) const noexcept;
//c-string (2)
size_type find_first_not_of (const charT* s, size_type pos = 0) const;
//buffer (3)
size_type find_first_not_of (const charT* s, size_type pos, size_type n) const;
//character(4)
size_type find_first_not_of (charT c, size_type pos = 0) const noexcept;

說明:
在源串中從位置pos開始往後查找,只要在源串遇到一個字符,該字符與目標串中的任意一個字符都不相同,就停止查找,返回該字符在源串中的位置;若遍歷完整個源串,都找不到滿 足條件的字符,則返回npos。

示例(僅簡單舉例,有了前邊的學習,相信讀者可以自己學習find_first_not_of()):

#include<iostream>
#include<string>

using namespace std;

int main()
{
//測試size_type find_first_not_of (const charT* s, size_type pos = 0) const;
string str("abcdefg");
cout << str.find_first_not_of("kiajbvehfgmlc", 0) << endl;//3 從源串str的位置0(a)開始查找,目標串中有a(匹配),再找b,b匹配,再找c,c匹配,
// 再找d,目標串中沒有d(不匹配),停止查找,返回d在str中的位置3
return 0;
}

將前面應用舉例中的fing_first_of()換成find_first_not_of(),就可以將字符串中所有非元音字母換成*。請讀者自己驗證。

6.find_last_not_of()

原型:


//string (1)
size_type find_last_not_of (const basic_string& str, size_type pos = npos) const noexcept;
//c-string (2)
size_type find_last_not_of (const charT* s, size_type pos = npos) const;
//buffer (3)
size_type find_last_not_of (const charT* s, size_type pos, size_type n) const;
//character (4)
size_type find_last_not_of (charT c, size_type pos = npos) const noexcept;

說明:
find_last_not_of()與find_first_not_of()相似,只不過查找順序是從指定位置向前,這裡不再贅述,相信讀者可以自行學習。

Copyright © Linux教程網 All Rights Reserved