天蓬老师2017-04-17 11:48:58
封装一下 find
方法即可。
随手写的示例代码,仅供参考:
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;