我想检查第一个字符串包含第二个字符串的次数。 我在网上读到它,我发现了一个名为 std::find 的函数,我尝试使用它但我失败了...
std::string Str1 = "Hello Hello";
std::string Str2 = "ll";
Now what?
我试过
std::count
同样,但我发现它只对字母有效。
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());
}
关于c++ - 查找字符串 1 包含字符串 2 C++ 的次数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53036976/