我正在努力寻找一个子串在一个stings中的位置数。下面的代码只为我提供了字符串中子字符串的起始位置。请帮我。
void Search(std::string SubString, vector<string> index)
{
vector<string>::const_iterator cii;
string temp;
bool found = false;
unsigned find = 0;
for(cii = index.begin(); cii != index.end(); cii++)
{
temp = *cii;
find = temp.find(SubString);
if(find != std::string::npos)//running.find()
{
cout << find << endl;
cout << *cii << endl;
found = true;
}
else
{
if((cii == index.end()) && found)
{
cout << "Not found\n";
found = false;
}
}
}
}
int main()
{
vector<string> index;
ifstream myReadFile;
string str, running;
myReadFile.open("example.txt");
char output[100];
if (myReadFile.is_open())
{
while (!myReadFile.eof())
{
while( std::getline( myReadFile, str ) )
{
index.push_back(str);
}
}
}
while(running != "END")
{
cout << "To Quit type:- END\n";
cin >> running;
if(running == "END")
{break;}
else
{
Search(running, index);
}
}
myReadFile.close();
return 0;
}