封装一下 find 方法即可。 随手写的示例代码,仅供参考: cppstd::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;
封装一下
find方法即可。随手写的示例代码,仅供参考: