string text {"Smith, where Jones had had "had", had had "had had"."" "Had had" had had the examiners' approval."}; std::cout << text << std::endl; string phrase {"had had"}; size_t count {}; auto iter = std::begin(text); auto end_iter = end(text); while((iter = std::search(iter, end_iter, std::begin(phrase), std::end (phrase) , [](char ch1, char ch2) { return std::toupper (ch1) == std:: toupper (ch2); })) != end_iter) { ++count; std::advance(iter, phrase.size()); // Move to beyond end of subsequence found } std::cout << "n""<< phrase << "" was found "<< count << " times." << std::endl;這段程式碼執行後會輸出下面的內容:
Smith, where Jones had had "had", had had "had had". "Had had" had had the examiners' approval.
"had had" was found 5 times.