find顺搜索
rfind逆搜索
string::npos表示母串中不含字串
index表示位置
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
void main()
{
string text = "had had sdsdsdhad";
string word = "had";
int count = 0;
for (int index = 0; (index = text.find(word, index)) != string::npos; index = word.length() + index, count++);
cout << count;
system("pause");
return;
}
网友评论