find()函数

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;
}
原文地址:https://www.cnblogs.com/freinds/p/6345165.html