1. 程式人生 > >水題 codeforces 133A - HQ9+ && sting類find系列函數簡單總結

水題 codeforces 133A - HQ9+ && sting類find系列函數簡單總結

三個參數 寫上 c_str 字符串 系列 har 一次 初始 abcd

題意:給出一個字符串,判斷裏面有沒有‘H’‘Q’ ‘9’三者中的任何一個,如果有的話輸出“YES”,否則輸出“NO”
思路:利用string類的find函數進行查找即可
code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cin >> s;
string t("HQ9");
if(s.find_first_of(t)!=-1)printf("YES\n");
else printf("NO\n");
return 0;
}
題目本身並無難度,主要是借此機會對string類的find函數系列進行簡單的總結: 總述:
string查找函數,都有唯一的返回類型——size_type,即一個無符號整數(按打印出來的算)。查找成功,返回按查找規則找到的第一個字符或子串的位置;查找失敗,返回npos,即-1(打印出來為4294967295)。 1.find()
原型:
//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; 不嚴謹地總結一下(簡單粗暴地理解):
find()函數最多可以有三個參數:尋找的內容,初始位置,尋找內容再調整
尋找的內容:
可以是單字符,也可以是字符串;可以是事先定義好的字符串,也可以直接寫上去。
初始位置:
如果初始位置超出字符串長度時,按查找失敗處理;只有兩個參數時省略表示從位置0開始
尋找內容再調整:
尋找內容的前n個字符參與匹配,超出第一個參數長度時,按失敗處理
返回值:
查找失敗時返回npos(-1或者4294967296----即均表示32個1)
示例: #include<iostream>
#include<string>
using namespace std;
int main()
{
string st1("babbabab");
cout << st1.find(‘a‘) << endl;//1
cout << st1.find(‘a‘, 0) << endl;//1
cout << st1.find(‘a‘, 1) << endl;//1
cout << st1.find(‘c‘, 0) << endl;//4294967295
cout << (st1.find(‘c‘, 0) == -1) << endl;//1
cout << (st1.find(‘c‘, 0) == 4294967295) << endl;//1
cout << st1.find(‘a‘, 100) << endl;//4294967295
string st2("aabcbcabcbabcc");
string str1("abc");
cout << st2.find(str1, 2) << endl;//6
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;
} 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; 與find()類似,但是查找順序不一樣,rfind()是從指定位置向前查找,如,上例中的st1.rfind(‘a‘,7)一句,就是從st1的位置7(st1的最後一個字符b)開始查找字符a,第一次找到的是倒數第2個字符a,所以返回6。 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
示例: #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()類似,查找順序從指定位置向前 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;
與find_first_of()類似,在源串中從位置pos開始往後查找,只要在源串遇到一個字符,該字符與目標串中的任意一個字符都不相同,就停止查找,返回該字符在源串中的位置;若遍歷完整個源串,都找不到滿 足條件的字符,則返回npos。 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_first_not_of()相似,只不過查找順序是從指定位置向前
部分代碼來自

水題 codeforces 133A - HQ9+ && sting類find系列函數簡單總結