C++ 中string.find() 函数的用法总结 - hgfzdd

hgfgood 2021-08-05 原文


C++ 中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,这是一个很大的数,其值不需要知道。

LOFTER:hgfalgorithm   http://hgfal.lofter.com/post/28eef2_f0d74b

posted on
2014-03-12 14:00 
hgfzdd 
阅读(1260
评论(0
编辑 
收藏 
举报

 

版权声明:本文为hgfgood原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/hgfgood/p/4248321.html

C++ 中string.find() 函数的用法总结 - hgfzdd的更多相关文章

  1. Windows Server 2008中用管理员的权限使用命令行来打开程序 – 中道学友

    Windows Server 2008中用管理员的权限使用命令行来打开程序 Windows Server 20 […]...

  2. JS 实现显示和隐藏div(以百度地图为例) – anyuan9

    JS 实现显示和隐藏div(以百度地图为例) 主要参考的文章:https://my.oschina.net/x […]...

  3. 怎样能对众人演讲讲话不紧张怯场 – ohmyjava

    怎样能对众人演讲讲话不紧张怯场 4月27日 20:35 爱默生说:“恐惧较之世上任何事物更能击溃人类。”诚然, […]...

  4. ListBox控件使用 – 阿社

    ListBox控件使用 ListBox 可以通过ListSelectionMode 设置多选,默认值为 Sin […]...

  5. Multisim14.0(01) – 维特根斯坦

    Multisim14.0(01) 软件仿真 1.17  研究电路在R的阻值变化时二极管的直流电压和交流电压的变 […]...

  6. C# 插入文本框到PPT幻灯片

    概述 在文本框中我们可以实现的操作有很多,如插入文字、图片、设置字体大小、颜色、文本框背景填充、边框设置等。下 […]...

  7. 史上最强防火墙iptables – 生活费

    史上最强防火墙iptables #1.清空所有的防火墙规则 iptables -F iptables -X i […]...

  8. ARCGIS对谷歌影像进行投影转换 – 于谦儿子郭小宝

    相信有不少同学会有这样的困扰,通过软件下载的谷歌遥感影像,直接用ARCGIS等专业软件打开之后发现,遥感影像有 […]...

随机推荐

  1. PTA 创建计算机类

    6-5创建计算机 (10分) 定义一个简单的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱 […]...

  2. Markdown的基本使用指南

    目录 1.标题 2.列表 2.1无序列表 2.1有序列表 3.引用 4.图片和链接 5.粗体和斜体 6.分割线 […]...

  3. MongoDB 中数据的替换方法实现 –类Replace()函数功能

    关键字: MongoDB,Replace,forEach 近日接到一个开发需求,因业务调整,需要DBA协助,将 […]...

  4. (转) 使用Speech SDK 5.1文字转音频

    下载地址: http://www.microsoft.com/en-us/download/details.a […]...

  5. Fidder详解之get和post请求

    本文向大家详细讲述了Fidder的常用功能,希望大家通过本文的阅读,能使你进步!   前言   本文会对Fid […]...

  6. mac自带录屏

    MacOS Mojave以上系统:   【Shift+Command+5】即开启录制屏幕       点击下方 […]...

  7. Spring Boot 中使用 Quartz 实现任务调度

    Quartz 概述 Quartz 是 OpenSymphony 开源组织在 Job Scheduling 领域 […]...

  8. Firefox 浏览器有用的插件

    1、Undo Closed Tabs Button或Undo Closed Tabs Button (revi […]...

展开目录

目录导航