STL string中find()的用法

STL的string中函数find(),用来返回子串sub在主串str中出现的位置(比kmp要快)

#include<string>
#include<iostream>
using namespace std;
int main()
{
    string str,sub;
    cin>>str;
    cin>>sub;
    int pos=str.find(sub);
    if(pos!=string::npos)
        cout<<"sub串在str串中的位置是"<<pos<<endl;
    else 
        cout<<"sub串在str串中未出现"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/why932346109/article/details/89303499