string::npos参数 —— npos 是一个常数,用来表示不存在的位置

例如,有两个字符串a、b,判断a字符串是否包含b字符串

//如果字符串不存在包含关系,那么返回值就一定是npos
if(a.find(b)!=string::npos){cout<<"yes"<<endl;
}else{cout<<"no"<<endl;
}

string::size_type pos;

str.find("字符串") 返回值是字符在母串s中的下标位置;

str.find("字符串",9) 从s的下标9开始,查找字符串,返回字符串在s中的下标;

pos=s.find(str,pos) 查找s中str出现的所有位置。

pos=s.find_first_of(str)  返回str出现在母串s中的首次出现的位置

pos=s.find_last_of(str)  返回str出现在母串s中的最后一次出现的位置

#include<iostream>
#include<string>
using namespace std;
int main(){string s("abcbcdcd");string str="cd";string::size_type pos;pos=s.find("bc");if(pos!=s.npos){cout<<pos<<endl;//1}else{cout<<"no"<<endl;}pos=s.find("bc",2);cout<<"从s下标2开始查找:"<<pos<<endl;//3 int i=1;while((pos=s.find(str,pos))!=string::npos){cout<<"位置"<<i<<":"<<pos<<endl;//位置1:4  位置2:6 pos++;i++; }cout<<"第一次:"<<s.find_first_of('d')<<endl;//5cout<<"最后一次:"<<s.find_last_of('d')<<endl;//7
}

erase函数的原型如下:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
也就是说有三种用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)

#include<iostream>
#include<string>
using namespace std;
int main(){string s("I love zufe.");string::iterator it;s.erase(1,5);//删除从1开始的5个字符 cout<<s<<endl;//I zufe.it=s.begin()+6;s.erase(it);//删除位置为9的字符 cout<<s<<endl;//I zufes.erase(s.begin()+3,s.end()-2);//删除之间的字符 cout<<s<<endl;//I zfe
}

string find()函数、string::npos的含义、erase()函数相关推荐

  1. string find()函数、string::npos的含义、erase()函数

    叮叮加粗样式~~ 一.string find()函数 第一种,algorithm头文件的find(). 使用方法:find(begin,end,value) 解释:第一个参数是容器或者数组的起始地址( ...

  2. C++中string::相关函数【s.find()、s.npos、s.substr()、 isdigit(c)、stoi(s),erase()函数】

    string find()函数.string::npos的含义.erase()函数 1. string::find()函数和string::npos函数的介绍 2 erase()函数 3 字符串截取 ...

  3. C++中string erase函数的使用

    erase函数的原型如下: (1)string& erase ( size_t pos = 0, size_t n = npos ); (2)iterator erase ( iterator ...

  4. string类的erase函数属于stl吗_C++ STL快速入门

    C++ STL中最基本以及最常用的类或容器无非就是以下几个:在数月之前的机试中第一次体验到STL的威力,因为自己本来一直在用C语言做开发,很多数据结构都是自己造的,比如链表.队列等,第一次接触C++ ...

  5. string.find()与string::npos

    C++: string 中find函数的用法以及string::npos的含义_linwh8的博客-CSDN博客_c++ npos 查找字符串a是否包含子串b,不是用strA.find(strB) & ...

  6. C++ STL笔记二:string容器;string构造函数;string赋值函数;string拼接;string查找、替换;string比较;string单个字符存取;string插入、删除、截取

    本质: string是C++风格的字符串,而string本质上是一个类. string和char*区别: char*是一个指针 string是一个类,类内部封装了char*,管理这个字符串,是一个ch ...

  7. C++ STL函数 string(henu.hjy)

    string是字符串 前提:#include <string> (C中应加入using namespace std:) 注释中给了各种string函数的用法,大家可以借鉴一下 #inclu ...

  8. C++查找子串string.find()与string::npos

    string.find() string str = "abc"; string subStr1 = "bc"; string subStr2 = " ...

  9. c++处理字符串string.find()与string::npos

    在c++中,字符串类型string后面加上.find()的作用 演示 #include<iostream> #include<cmath> #include<algori ...

最新文章

  1. git 只merge部分_[Skill]俩小时掌握多人开发中git的主要用法
  2. SAP HUM 带HU的TO单对应的外向交货单VL09取消PGI之后不能对该交货单执行LT0G做WM层面的返架?
  3. 在yii2中,让你action参数支持POST数据的小方法
  4. 中国工程院谭建荣:人工智能应用得再好,最核心的算法不行,创新能力就不行
  5. Java8 LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互转、Date与LocalDateTime互转
  6. ubuntu16.04/20.04 xfce4下面使用护眼软件redshift
  7. 机器学习模型开发必读:开源数据库最全盘点
  8. JAVA集合(笔记)
  9. Codeforces 384E-线段树+dfs序
  10. MSI/MSI-X Capability结构 (转)
  11. 用Python解压tgz文件
  12. day6:vcp考试
  13. 双边滤波器和高斯滤波器
  14. 主成分分析法确定权重
  15. Python 教程推荐
  16. 如何使用python刷博客浏览量---第一种方法
  17. 【萌新笔记】简单记录“智能管家”的设计过程
  18. 《带着神兽自学少儿编程》之01:爸爸下载Scratch【关注可畅读】
  19. sql中向下取整怎么取_SQL中的取整函数FLOOR、ROUND、CEIL、TRUNC、SIGN
  20. C 头歌 字符串插入

热门文章

  1. java数组的时间复杂度_java时间复杂度计算
  2. 先卸载 nvidia-387.26驱动,再安装nvidia-384.81 驱动
  3. 十一课堂|通过小游戏学习Ethereum DApps编程(1)
  4. Linux中使用gzip来压缩/解压 *.gz文件
  5. Android Studio一直停留在MyApplication:syncing(解决方案)
  6. Python迭代器、生成器、map以及reduce
  7. 技巧:Vimdiff 使用
  8. os.listdir()详解
  9. docker 导出本地所有镜像,带仓库和tag值导出到tar.gz文件
  10. mysql不等于null和等于null的写法