扫码关注官方订阅号
C++ 如何判断一个大字符串里出现一个小字符串的次数 用string对象的自带函数有吗
走同样的路,发现不同的人生
封装一下 find 方法即可。
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;
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;
写个例子把 比如abcabcabcdsadg里出现abc的次数
使用KMP算法或者sunday算法都可以处理
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
封装一下
find
方法即可。随手写的示例代码,仅供参考:
写个例子把 比如abcabcabcdsadg里出现abc的次数
使用KMP算法或者sunday算法都可以处理