string find的用法详解

/*
string 
        find()的使用  


#include
#include
using namespace std;

int main(){
    string st1=("babbabab");

    //从前面找a的位置

    cout<'a')<<"   ";  cout<<"输出第一个a的下标  (从0开始) "<'a',0)<<"     ";cout<<"('a',x) 从x下标开始查询a的下标"<//从后面找a 的位置 
    cout<'a',1)<<"     ";cout<<"从0 到 x 从后向前查找a 所在该串的位置"<'c',0)==-1)<//该数字不存在 就满足条件 为真 1     两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制)

    //st1.find("y",x); 该y可以是字符串 字符 string char型 均可 

    cout<"bababa",0,4)<//6   第三个参数不得超过第一个参数 

    return 0;
}



find() 的实例 
#include
#include
using namespace std; 
int main(){
    string str("babccbabcaabcc");

    int num=0;

    size_t fi=str.find("abc",0); 

    while(fi!=str.npos){ //没有找到一个 需要找到的位置 
        cout<" ";
        num++; //统计一共有几个下标 
        fi=str.find("abc",fi+1); //输出下一个下标 
    }
    if(0==num) cout<<"not find!";
    cout<return 0;
}



*/

// find_first_of() 的用法 

// 同样  find_last_of(str,x);  意思是从 x向前找 如果存在一个与str中相同的 则输出该下标 
// 

//这里不再演示 find_first_not_of() find_last_not_of()的用法了 其作用是
//如果该字符在str中没有出现就输出该下标 

#include
#include
using namespace std;

int main(){
    string str("babccbabcc");
    cout<'a',0);//1
    cout<'a',0)<//1 从0开始只要找到a 就输出该a的小标

    string str1("bvgjhikl");
    string str2("kghlj");
    cout<0)<//从str1的第0个字符开始 过程就是 b在str2中是否存在 如果存在就直接输出该下标
    //在找v是否在str2中是否存在 存在就输出改下标

    cout<"kghlj",0,20);//2 第三个参数超过 

你可能感兴趣的:(字符串,模板---STL)