天蓬老师2017-04-17 11:48:58
Encapsulate the find
method.
Handwritten sample code for reference only:
cpp
std::string str("abcabdabcdsdabcds"); auto occurrences = [&str](const std::string &dest) { size_t pos, pre = 0, count = 0; while ( (pos = str.find(dest, pre)) != std::string::npos ) { ++count; pre = pos + 1; } return count; }; std::cout << occurrences("abc") << std::endl;
PHP中文网2017-04-17 11:48:58
Write an example, such as the number of times abc appears in abcabcabcdsadg