所以我有这个,如果我注释掉底部,从int count = 0;
到return 0;
它会打印,但在这种情况下,没有打印出来。甚至在开始时加入cout << "Test"
什么都不做。它虽然编译好。为什么这不打印任何东西
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string text = "Smith, where Jones had had \"had had\", had had \"had\". \"Had had\" had had the examiners' approval.";
string search = "had";
int length = (int) text.length();
for(int i = 0; i < length; i++)
{
text [i] = tolower(text [i]);
}
cout << text;
int count = 0;
for (int index = 0; (index = text.find(search)) != string::npos; index += search.length()) {
count++;
}
cout << "There are " << count << " occurences of \"" << search << "\".\n";
return 0;
}
你的最后一个for循环是一个无限循环,因为'search'在'text'总能找到。 – Meysam 2013-02-16 05:28:26