string 中find_first_of()函数用法例题

s.find_first_of(args)在s中查找args中的任意字符第一次出现的位置,结果返回string::size_type类型的变量,即查找到位置,下标从0开始

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

int main() {
 string s("ab2c3d7R4E6");
 string::size_type pos=0;
 string num("0123456789");
 cout<<"数字位置:";
 string c("qwertyuiopasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM");
 while((pos=s.find_first_of(num,pos))!=string::npos)
 {
  cout<<pos<<" ";
  ++pos;
 }
 cout<<endl;
 pos=0;//注意pos要重新赋值,经过上述循环已达s的末尾,否则将找不到匹配位置
 cout<<"字符位置:";
 while((pos=s.find_first_of(c,pos))!=string::npos)
  {
   cout<<pos<<" ";
   ++pos;
  }
 cout<<endl;
return 0;

}

结果:

数字位置:2 4 6 8 10

字符位置:0 1 3 5 7 9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值