string中 find()的应用  (rfind() 类似,只是从反向查找)
原型如下:
(1)size_t find (const string& str, size_t pos = 0) const;  //查找对象--string类对象
(2)size_t find (const char* s, size_t pos = 0) const; //查找对象--字符串
(3)size_t find (const char* s, size_t pos, size_t n) const;  //查找对象--字符串的前n个字符
(4)size_t find (char c, size_t pos = 0) const;  //查找对象--字符
结果:找到 -- 返回 第一个字符的索引
     没找到--返回   string::npos
 
示例:
#include <iostream>       // std::cout#include <string>         // std::stringint main (){  std::string str ("There are two needles in this haystack with needles.");  std::string str2 ("needle");  // different member versions of find in the same order as above:  std::size_t found = str.find(str2);  if (found!=std::string::npos)    std::cout << "first 'needle' found at: " << found << '\n';  found=str.find("needles are small",found+1,6);  if (found!=std::string::npos)    std::cout << "second 'needle' found at: " << found << '\n';  found=str.find("haystack");  if (found!=std::string::npos)    std::cout << "'haystack' also found at: " << found << '\n';  found=str.find('.');  if (found!=std::string::npos)    std::cout << "Period found at: " << found << '\n';  // let's replace the first needle:  str.replace(str.find(str2),str2.length(),"preposition");  //replace 用法  std::cout << str << '\n';  return 0;}
结果:
first 'needle' found at: 14
second 'needle' found at: 44
'haystack' also found at: 30
Period found at: 51
There are two prepositions in this haystack with needles
 
 
其他还有  find_first_of(), find_last_of(), find_first_not_of(), find_last_not_of()
作用是查找   字符串中 任一个字符 满足的查找条件
string snake1("cobra");
int where = snake1.find_first_of("hark");
返回3  因为 "hark"中 各一个字符 在 snake1--cobra 中第一次出现的是  字符'r'(3为 cobra 中'r'的索引)
同理:
int where = snake1.find_last_of("hark");
返回4  因为 "hark"中 各一个字符 在 snake1--cobra 中最后一次出现的是  字符'a'(3为 cobra 中'r'的索引)
 

其他同理

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

C++ string中find ,rfind 等函数 用法总结及示例相关推荐

  1. string中find和find_first_of函数用法总结

    一:find 函数原型: size_t find ( const string& str, size_t pos = 0 ) const; size_t find ( const char* ...

  2. getservbyname php,php中getservbyport与getservbyname函数用法实例

    本文实例讲述了php中getservbyport与getservbyname函数用法.分享给大家供大家参考.具体如下: string getservbyport ( int $port , strin ...

  3. python中内置函数的用法_python中str内置函数用法总结

    大家在使用python的过程中,应该在敲代码的时候经常遇到str内置函数,为了防止大家搞混,本文整理归纳了str内置函数.1字符串查找类:find.index:2.字符串判断类:islower.isa ...

  4. java split函数的用法,java拆分字符串_java中split拆分字符串函数用法

    摘要 腾兴网为您分享:java中split拆分字符串函数用法,中信期货,掌上电力,星球联盟,淘集集等软件知识,以及韩剧精灵,每日英语听力vip,龙卷风收音机,优衣库,中国平煤神马集团协同办公系统,光晕 ...

  5. python threading join_Python中threading模块join函数用法实例分析

    本文实例讲述了Python中threading模块join函数用法.分享给大家供大家参考.具体分析如下: join的作用是众所周知的,阻塞进程直到线程执行完毕.通用的做法是我们启动一批线程,最后joi ...

  6. C++string中的insert()插入函数

    C++string中的insert()插入函数 basic_string& insert (size_type pos, const basic_string& str); 在原串下标 ...

  7. Mysql中rank类的函数用法

    Mysql中rank类的函数用法 rank() over 作用:查出指定条件后的进行排名,条件相同排名相同,排名间断不连续. 说明:例如学生排名,使用这个函数,成绩相同的两名是并列,下一位同学空出所占 ...

  8. C#中ToInt32以及类似函数用法介绍

    C#中ToInt32以及类似函数用法介绍 作用 程序举例 程序逻辑 程序代码 程序 作用 将指定的值转换为 32 位有符号整数.对应的还有ToInt16,ToInt64 指定的值可以是字符串.时间.位 ...

  9. python中的内置函数getattr()介绍及示例

    python中的内置函数getattr()介绍及示例 其实getattr()这个方法最主要的作用是实现反射机制.也就是说可以通过字符串获取方法实例.这样,你就可以把一个类可能要调用的方法放在配置文件里 ...

最新文章

  1. 烂泥:学习ssh之ssh无密码登陆
  2. mysql表名不区分大小写_设置mysql表名不区分大小写
  3. cnpm在ubuntu19.10下面的安装以及vue.js中el的意思
  4. 【CodeForces - 124C】Prime Permutation(数学,思维,小结论)
  5. linux设备驱动之串口移植,Linux设备驱动之UART驱动结构
  6. 用贪心策略均分纸牌(洛谷P1031题题解,Java语言描述)
  7. ISA2004升级到ISA2006需要注意的事项
  8. [Apache]安装中出现的问题
  9. java保护性拷贝(effective java)
  10. php程序员如何转go,写给 PHP 程序员的 Go 入门教程
  11. 头歌实践平台:机器学习——支持向量回归
  12. 电路原理 的 一些基础知识
  13. 我国跨境电子商务的发展概念以及发展概况
  14. 四川大学计算机考研信息汇总
  15. appcan与java_AppCan试用体验
  16. 计算机信函 教案模板,一年级信息技术课教案模板三篇
  17. Ubuntu的共享文件夹
  18. 国际期货和国内期货的区别带招商
  19. JME3 官方教程翻译 - 自定义网格形状
  20. C语言英文背单词软件,C语言背单词程序

热门文章

  1. Ubuntu版-virtualbox安装笔记
  2. 通过IIS操作修改服务器文件没有权限的解决办法
  3. Python零基础学习代码实践 —— 提取字符串里面的单词数
  4. DNS RR字段含义
  5. 编译程序加不加 -lpthread 的区别【转】
  6. 实测DB_BLOCK_CHECKSUM=FULL的作用
  7. Win7启动显示无法加载用户配置文件
  8. qt 使用非系统字库
  9. shell 全局和局部变量
  10. solrj 使用记录 - 添加文档