C++ 如何判断一个大字符串里出现一个小字符串的次数 用string对象的自带函数有吗
走同样的路,发现不同的人生
Encapsulate the find method.
find
Handwritten sample code for reference only:
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;
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;
Write an example, such as the number of times abc appears in abcabcabcdsadg
It can be processed using KMP algorithm or Sunday algorithm
Encapsulate the
find
method.Handwritten sample code for reference only:
Write an example, such as the number of times abc appears in abcabcabcdsadg
It can be processed using KMP algorithm or Sunday algorithm