string类提供 6 种搜索函数,其中每个函数有 4 个原型,简介如下:
F.1 find()系列C++11中 find()原型
- size_type find(const basic_string& str,size_type pos =0 )const noexcept;
第一个返回str在调用对象中第一次出现时的起始位置。搜索从pos开始,如果没有找到子字符串,将返回npos。
示例代码:
string longer = "That is a funny hat.";
string shorter = "hat";
size_type loc1 =longer.find(shorter); /// sets loc1 to 1
size_type loc2=longer.find(shorter,loc1 + 1); sets loc2 to 16
由于第二条搜索语句从位置2开始(that中的a),因此它找到的第一个hat位于字符串尾部,要测试是否失败,可以使用string::npos值:
if(loc1 == string::npos)
cout<<" Not found\n ";
- size_type find(const char s,size_type pos =0 )const*
第二个方法完成同样的工作,但它使用 字符数组而不是string对象作为子字符串:
size_type loc3 = longer.find("is"); ///sets loc3 to 5
- size_type find(const char* s,size_type pos,size_type n)const
第三个方法完成相同的工作,但它只使用字符串 s 的前 n 个字符。这与使用basic_string(const charT* s,size_type n)构造函数,然后将得到的对象用作第一种格式的find()的string 参数的效果完全相同,例如下面的代码搜索子字符串“fun”:
size_type loc4 = longer.find("funds",3); ///sets loc4 to 10
- size_type find(charT c,size_type pos=0)const noexcept;
第四种方法的功能与第一个相同,但是它使用一个字符而不是string对象作为子字符串:
size_type loc5 = longer.find('a'); ///sets loc5 to 2