++

总述:
      以下所讲的所有的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()相似,只不过查找顺序是从指定位置向前,这里不再赘述,相信读者可以自行学习。

c++中find函数解析相关推荐

  1. oracle中next_day()函数解析

    oracle中next_day()函数解析 Sql代码 当前系统时间的下一星期一的时间select   next_day(sysdate,1) from dual NEXT_DAY(date,char ...

  2. python中append函数解析_对python中的pop函数和append函数详解

    对python中的pop函数和append函数详解 pop()函数 1.描述 pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值. 语法 pop()方法语法: list. ...

  3. 【ROS学习】- tf学习 - tf中重要函数解析 (陆续更新....)

    文章目录 一.函数waitForTransform().lookupTransform() 具体解释 二.函数 tf::StampedTransform().sendTransform() 具体解释 ...

  4. 【晕头晕脑的Python】Python中Reshape函数解析

    Reshape函数解析 Reshape()作用: Reshape()实例说明: 一维reshape() 为 二维 二维数组 reshape 切片,逆置 三维Reshape情况 Reshape()作用: ...

  5. java中main函数解析

    作者:xwdreamer 出处:http://www.cnblogs.com/xwdreamer 欢迎任何形式的转载,但请务必注明出处. 从写java至今,写的最多的可能就是主函数 public st ...

  6. java中的de是什么_【转】java中main函数解析

    源地址:http://www.cnblogs.com/xwdreamer/archive/2012/04/09/2438845.html 从写java至今,写的最多的可能就是主函数 public st ...

  7. java中的主函数_java中main函数解析

    从写java至今,写的最多的可能就是主函数 public static void main(String[] args) {} 但是以前一直都没有问自己,为什么要这么写,因为在c语言中就没有这样子的要 ...

  8. main函数 java_java中main函数解析

    从写java至今,写的最多的可能就是主函数 public static void main(String[] args) {} 但是以前一直都没有问自己,为什么要这么写,因为在c语言中就没有这样子的要 ...

  9. c++中CreateEvent函数解析(1)

    函数原型: HANDLE CreateEvent(LPSECURITY_ATTRIBUTES lpEventAttributes, // SDBOOL bManualReset, // reset t ...

  10. ORB-SLAM2中的Loop Closinng中DetectLoopCandidates函数解析

    /函数的三要素是:函数返回值类型,函数名称,函数参数 函数的返回值是装有关键帧指针的vector 该函数是类KeyFrameDatabase的成员函数,函数名是DetectLoopCandidate ...

最新文章

  1. Linux下 C语言统计时间差
  2. switch中default的用法
  3. 服务器空岛怎么修改地形,迷你世界空岛地形码是什么 空岛地形码怎么输入[多图]...
  4. python的程序入口地址_第一个Python程序
  5. Truebine聚合监控
  6. apache flume_Flume:使用Apache Flume收集客户产品搜索点击数据
  7. Capture images using V4L2 on Linux
  8. css 浮动在最上层_《CSS 知识总结》
  9. 如何最快的获取QQ群所有成员的qq号信息
  10. 【优化算法】人工蜂鸟优化算法(AHA)【含Matlab源码 1470期】
  11. lopatkin俄大神精简中文系统Windows 7 Professional SP1 7601.24540 x64 ZH-CN LITE10
  12. 边际递减,边际成本,边际收益,边际效益
  13. es 同步期间数据更新_在大流行期间成为数据科学家的感觉如何
  14. Android Studio 中使用 Git
  15. 计算捐款总量 (10 分)
  16. MAC A1466 820-00165-A 进水不触发
  17. 阿里巴巴图标库的使用
  18. 网能云平台,掌聚网能助力机房运维的利器
  19. 计算机视觉基础--边缘检测
  20. 大型演唱会无线wifi网络覆盖解决方案

热门文章

  1. An internal error occurred during: Add Deployment.
  2. 解锁wp7手机实现真机调试
  3. 智能家居DIY创意之智能灯泡
  4. 移动软件开发定制那些事
  5. 正交子空间投影的学习笔记
  6. 可逆矩阵的秩等于矩阵的阶数_为什么矩阵的秩等于其行阶梯行矩阵非零行的行数?详细一点哈?谢了。...
  7. 大学生Web开发一大作业(静态网页)
  8. 企业中台最佳实践--阿里数据中台最佳实践(九)
  9. 高通平台WIFI-去掉信号标识上面的叹号和叉叉issue
  10. 教你用 Python 快速获取行业板块股,辅助价值投资!