水题 codeforces 133A - HQ9+ && sting类find系列函数简单总结

题意:给出一个字符串,判断里面有没有‘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()相似,只不过查找顺序是从指定位置向前
部分代码来自

猜你喜欢

转载自www.cnblogs.com/sqlbf/p/10325800.html
今日推荐