首页 > 其他 > 详细

库函数

时间:2017-01-24 00:01:43      阅读:189      评论:0      收藏:0      [点我收藏+]

find()函数返回类型:size_type

1/S.find(T):返回T在S中第一次匹配的下标位置 

2/S.find_first_of(T):返回字符串T第一个字符在S中第一次出现的下标位置 

3/S.find("T",2):从字符串S下标2开始,查找字符串“T”,返回在S中匹配的下标

4/T.find_first_not_of(S):查找T中第一个在S中找不到的字符在T中所在位置

5/S.rfind(T):反向查找,字符串T第一个字符在S中最后出现的位置

#include <string>
#include <iostream>
using namespace std;
int main()
{
     
    string S("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8h9i");
    string T;
    string::size_type pos;
    cout<<"S="<<S<<endl; 
    
    pos=S.find("4g");
     if(pos!=S.npos)//如果没找到,返回一个特别的标志c++中用npos表示 这里npos取值是4294967295
          cout<<"S.find(4g)"<<"position is:"<<pos<<endl;
     else
          cout<<"Not found the(4g)"<<endl;
    
     pos=S.find_first_of("6h");
     cout<<"S.find_first_of(6h)is:"<<pos<<endl;
    
     pos=S.find("2b",5);
     cout<<"S.find(2b,5)is:"<<pos<<endl;
    
     T="2b";
     pos=0;
     int i=1;//查找S中字符串T出现的所有位置。
     while((pos=S.find_first_of(T,pos))!=string::npos){
          cout<<"position"<<i<<":"<<pos<<endl;
          pos++,i++;
     }
    
     T="acb12389xefgxyz789";
     pos=T.find_first_not_of(S);
     cout<<"T.find_first_not_of(S):"<<pos<<endl;
    
     T="3c4d";
     pos=S.rfind(T);
     cout<<"S.rfind(3c4d):"<<pos<<endl;
}

 

库函数

原文:http://www.cnblogs.com/freinds/p/6345165.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!