string find简析
#include <string>
#include <iostream>
using namespace std;
void main()
{
- ////find函数返回类型 size_type
- string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
- string flag;
- string::size_type position;
- //find 函数 返回jk 在s 中的下标位置
- position = s.find("jk");
- if (position != s.npos) //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,
- {
- cout << "position is : " << position << endl;
- }
- else
- {
- cout << "Not found the flag" + flag;
- }
- //find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置
- flag = "c";
- position = s.find_first_of(flag);
- cout << "s.find_first_of(flag) is : " << position << endl;
- //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
- position=s.find("b",5);
- cout<<"s.find(b,5) is : "<<position<<endl;
- //查找s 中flag 出现的所有位置。
- flag="a";
- position=0;
- int i=1;
- while((position=s.find_first_of(flag,position))!=string::npos)
- {
- //position=s.find_first_of(flag,position);
- cout<<"position "<<i<<" : "<<position<<endl;
- position++;
- i++;
- }
- //查找flag 中与s 第一个不匹配的位置
- flag="acb12389efgxyz789";
- position=flag.find_first_not_of (s);
- cout<<"flag.find_first_not_of (s) :"<<position<<endl;
- //反向查找,flag 在s 中最后出现的位置
- flag="3";
- position=s.rfind (flag);
- cout<<"s.rfind (flag) :"<<position<<endl;
- }
说明:
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简析的更多相关文章
- RecycleView + CardView 控件简析
今天使用了V7包加入的RecycleView 和 CardView,写篇简析. 先上效果图: 原理图: 这是RecycleView的工作原理: 1.LayoutManager用来处理RecycleVi ...
- Android 启动过程简析
首先我们先来看android构架图: android系统是构建在linux系统上面的. 所以android设备启动经历3个过程. Boot Loader,Linux Kernel & Andr ...
- Android RecycleView + CardView 控件简析
今天使用了V7包加入的RecycleView 和 CardView,写篇简析. 先上效果图: 原理图: 这是RecycleView的工作原理: 1.LayoutManager用来处理RecycleVi ...
- Java Annotation 及几个常用开源项目注解原理简析
PDF 版: Java Annotation.pdf, PPT 版:Java Annotation.pptx, Keynote 版:Java Annotation.key 一.Annotation 示 ...
- 【ACM/ICPC2013】POJ基础图论题简析(一)
前言:昨天contest4的惨败经历让我懂得要想在ACM领域拿到好成绩,必须要真正的下苦功夫,不能再浪了!暑假还有一半,还有时间!今天找了POJ的分类题库,做了简单题目类型中的图论专题,还剩下二分图和 ...
- SimpleDateFormat使用简析
title: SimpleDateFormat使用简析 date: 2016-07-11 11:48:20 tags: Java SimpleDateFormat --- [转载自博客:http:// ...
- zxing二维码扫描的流程简析(Android版)
目前市面上二维码的扫描似乎用开源google的zxing比较多,接下去以2.2版本做一个简析吧,勿喷... 下载下来后定位两个文件夹,core和android,core是一些核心的库,android是 ...
- AFNetworking封装思路简析
http://blog.csdn.net/qq_34101611/article/details/51698473 一.AFNetworking的发展 1. AFN 1.0版本 AFN 的基础部分是 ...
- [转载] Thrift原理简析(JAVA)
转载自http://shift-alt-ctrl.iteye.com/blog/1987416 Apache Thrift是一个跨语言的服务框架,本质上为RPC,同时具有序列化.发序列化机制:当我们开 ...
随机推荐
- android window(二)从getSystemService到WindowManagerGlobal
在Activity调用getSystemService(WINDOW_SERVICE) 调用的是父类ContextThemeWrapper package android.view; public c ...
- 使用Dockerfile docker tomcat部署
在百度上试很多文章都不行,只有这篇可以. 宿主机为:centos64位 //安装docker 1:yum install docker //启动docker 2:systemctl start do ...
- Easy Touch 摇感控制人物移动
Easy Touch 摇感控制人物移动 public class joystick : MonoBehaviour { public float Speed; //定义速度 p ...
- Andrew Ng 的 Machine Learning 课程学习 (week3) Logistic Regression
这学期一直在跟进 Coursera上的 Machina Learning 公开课, 老师Andrew Ng是coursera的创始人之一,Machine Learning方面的大牛.这门课程对想要了解 ...
- statfs获得硬盘使用情况 模拟linux命令 df
转自:http://blog.csdn.net/mociml/article/details/5335474 先说statfs结构:#include <sys/vfs.h> /* 或 ...
- Scroller类的使用总结
Scroll类之所以不好理解是因为没有搞清楚View的绘制流程. 1)简单来讲 viewgroup重绘时依次会调用 dispatchDraw -- drawChild --child.compute ...
- MySQL锁行锁表
select..for update; 给数据库表手动上锁 --锁行Begin; for update; --给 id=1 的行加上排它锁且 id 有索引 ; Commit; -- 锁表 BEGIN; ...
- [Java][Liferay] 如何从Javascript的function中获取language property的值
问题描述 在Portlet中,Javascript中通过Liferay.Language.get("key")的方式是拿不到自己添加的property的值,原因是Liferay.L ...
- SAP常用函数
1.获取月末最后一天日期 DATA LAST_DATE TYPE SY-DATUM. CALL FUNCTION 'LAST_DAY_OF_MONTHS' EXPORTING day_in = sy- ...
- python3绘图示例6-2(基于matplotlib,绘图流程介绍及设置等)
#!/usr/bin/env python# -*- coding:utf-8 -*- import os import numpy as npimport matplotlib as mpltfro ...