c++ - 一个函数来查找字符串 1 包含字符串 2 多少次 c++
问题描述
我想检查我的第一个字符串包含第二个字符串的次数。我在互联网上读到了它,我找到了一个函数名std::find,我尝试使用它,但失败了......
std::string Str1 = "Hello Hello";
std::string Str2 = "ll";
Now what?
我试着用
标准::计数
也是,但我发现它只是在一个字母上工作。
counter = std::count(Str1.begin(), Str2.end(), Str2); // dident work
帮助??
编辑:这就是我想要做的:
unsigned int Nucleus::get_num_of_codon_appearances(const std::string& codon) const
{
unsigned int counter = 0;
counter = std::count(this->_DNA_strand.begin(), this->_DNA_strand.end(), codon);
return counter;
}
解决方案
如果您使用的是 c++11 或更高版本,则可以使用 std::regex 轻松完成此操作。
就像是,
#include <regex>
#include <iterator>
#include <iostream>
#include <string>
using namespace std;
int main()
{
const string str = "one two hello three four hello five hello";
regex re("hello");
cout << "Number of hellos : " <<
distance(sregex_iterator(str.begin(),str.end(), re),sregex_iterator());
}
推荐阅读
- ios - 更新 FBSDK 11.2.0 后找不到“FBSDKCoreKitImport.h”文件
- python - gaierror: [Errno -2] 名称或服务未知| pysftp
- reactjs - 反应组件不显示在服务器上
- mysql - Mysql 不支持事务前隔离级别的任何设计原因?
- java - 跨度未附加到他的父上下文
- powershell - 我可以使用 PowerShell 知道我使用的 DNS 服务器是否是域控制器吗?
- github - Github Desktop:Rebase vs Merge Commit,让分叉保持最新
- java - 使用 @Value 时如何使用 Mockito 模拟地图
- python - 返回python中用户制作容器的切片类型
- javascript - 更智能的代码可避免重复以在一页中控制多个播放器