原文链接

#include <string>
#include <iostream>
using namespace std;

void main()
{

  1. ////find函数返回类型 size_type
  2. string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
  3. string flag;
  4. string::size_type position;
  5. //find 函数 返回jk 在s 中的下标位置
  6. position = s.find("jk");
  7. if (position != s.npos)  //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,
  8. {
  9. cout << "position is : " << position << endl;
  10. }
  11. else
  12. {
  13. cout << "Not found the flag" + flag;
  14. }
  1. //find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置
  2. flag = "c";
  3. position = s.find_first_of(flag);
  4. cout << "s.find_first_of(flag) is : " << position << endl;
  1. //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
  2. position=s.find("b",5);
  3. cout<<"s.find(b,5) is : "<<position<<endl;
  1. //查找s 中flag 出现的所有位置。
  2. flag="a";
  3. position=0;
  4. int i=1;
  5. while((position=s.find_first_of(flag,position))!=string::npos)
  6. {
  7. //position=s.find_first_of(flag,position);
  8. cout<<"position  "<<i<<" : "<<position<<endl;
  9. position++;
  10. i++;
  11. }
  1. //查找flag 中与s 第一个不匹配的位置
  2. flag="acb12389efgxyz789";
  3. position=flag.find_first_not_of (s);
  4. cout<<"flag.find_first_not_of (s) :"<<position<<endl;
  1. //反向查找,flag 在s 中最后出现的位置
  2. flag="3";
  3. position=s.rfind (flag);
  4. cout<<"s.rfind (flag) :"<<position<<endl;
  5. }

说明:

1.  如果string sub = ”abc“;

string s = ”cdeabcigld“;

s.find(sub) , s.rfind(sub) 这两个函数,如果完全匹配,才返回匹配的索引,即:当s中含有abc三个连续的字母时,才返回当前索引。

s.find_first_of(sub),   s.find_first_not_of(sub),   s.find_last_of(sub),  s.find_last_not_of(sub)  这四个函数,查找s中含有sub中任意字母的索引。

2.  如果没有查询到,则返回string::npos,这是一个很大的数,其值不需要知道。

string find简析的更多相关文章

  1. RecycleView + CardView 控件简析

    今天使用了V7包加入的RecycleView 和 CardView,写篇简析. 先上效果图: 原理图: 这是RecycleView的工作原理: 1.LayoutManager用来处理RecycleVi ...

  2. Android 启动过程简析

    首先我们先来看android构架图: android系统是构建在linux系统上面的. 所以android设备启动经历3个过程. Boot Loader,Linux Kernel & Andr ...

  3. Android RecycleView + CardView 控件简析

    今天使用了V7包加入的RecycleView 和 CardView,写篇简析. 先上效果图: 原理图: 这是RecycleView的工作原理: 1.LayoutManager用来处理RecycleVi ...

  4. Java Annotation 及几个常用开源项目注解原理简析

    PDF 版: Java Annotation.pdf, PPT 版:Java Annotation.pptx, Keynote 版:Java Annotation.key 一.Annotation 示 ...

  5. 【ACM/ICPC2013】POJ基础图论题简析(一)

    前言:昨天contest4的惨败经历让我懂得要想在ACM领域拿到好成绩,必须要真正的下苦功夫,不能再浪了!暑假还有一半,还有时间!今天找了POJ的分类题库,做了简单题目类型中的图论专题,还剩下二分图和 ...

  6. SimpleDateFormat使用简析

    title: SimpleDateFormat使用简析 date: 2016-07-11 11:48:20 tags: Java SimpleDateFormat --- [转载自博客:http:// ...

  7. zxing二维码扫描的流程简析(Android版)

    目前市面上二维码的扫描似乎用开源google的zxing比较多,接下去以2.2版本做一个简析吧,勿喷... 下载下来后定位两个文件夹,core和android,core是一些核心的库,android是 ...

  8. AFNetworking封装思路简析

    http://blog.csdn.net/qq_34101611/article/details/51698473 一.AFNetworking的发展 1. AFN 1.0版本 AFN 的基础部分是 ...

  9. [转载] Thrift原理简析(JAVA)

    转载自http://shift-alt-ctrl.iteye.com/blog/1987416 Apache Thrift是一个跨语言的服务框架,本质上为RPC,同时具有序列化.发序列化机制:当我们开 ...

随机推荐

  1. Dev Express Report 学习总结(五)在分组中使用聚集表达式AggregateExpression

    聚集表达式AggregateExpression主要包括几种:Avg(),Count(),Exists(),Max(),Min(),Single()和Sum().其中对于Sum(),在我看来主要有两种 ...

  2. python面向对象实例

    ——王宇阳 总结 (Code_boy)2018年11月16日 class Person: name='xxx' age=20 p=Person() #p为实例对象 print(p.name,p.age ...

  3. shell查看内存

    <1>jps<2>ps<3>free<4>df<5>top jps: 很多Java命令都在jdk的JAVA_HOME/bin/目录下面,jp ...

  4. STM32 从M3到M4

    一 考虑STM32不同系列移植的外设资源情况: STM32微控制器应用的移植和兼容性指南AN3364 二 M4的DSP/FPU的使用方法https://blog.csdn.net/electrocra ...

  5. express中connect-flash中间件的使用

    在学习node的时候,flash是困扰我最久的一个中间件,之前一直都没有很好的理解,这里做一个总结. 参考自:http://yunkus.com/connect-flash-usage/ 什么是fla ...

  6. express --- session详解

    之前一直做前端相关的工作,所以不太清楚session,也没有主动了解,最近在学node,对session的认识又有所加深,故总结之. 注: 关于session的一些配置问题,可以看这里. 第一部分: ...

  7. 07-spring之三大框架的整合

    1 三大框架整合理论 2 导包(42个) 1 hibernate 1 hibernate/lib/required 2 hibernate/lib/jpa | java persist api jav ...

  8. RSA算法、SSL协议学习笔记

    最近学习计算机网络,涉及到SSL协议,我想起了去年密码学课程讲过的非对称加密RSA算法,结合阮老师的博客,写写学习笔记,这里再回忆一下. RSA算法 RSA算法是一种非对称密码算法,所谓非对称,就是指 ...

  9. Andrew Ng 的 Machine Learning 课程学习 (week3) Logistic Regression

    这学期一直在跟进 Coursera上的 Machina Learning 公开课, 老师Andrew Ng是coursera的创始人之一,Machine Learning方面的大牛.这门课程对想要了解 ...

  10. POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】

    Jamie's Contact Groups Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...