【问题标题】:A function to find how many times string 1 contains string 2 c++一个函数来查找字符串 1 包含字符串 2 多少次 c++
【发布时间】:2019-04-01 21:09:45
【问题描述】:

我想检查我的第一个字符串包含第二个字符串的次数。 我在网上读到它,我找到了一个函数名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;
}

【问题讨论】:

  • 请说明您如何尝试使用std::find()
  • 我没有发现我的问题很有用..
  • 编辑:添加了我的尝试
  • 为了澄清一个要求,当你找到一个匹配时,你是从下一个字符重新开始还是从匹配的结尾开始?举个具体的例子,AAA 是否包含一个或两个AAs?
  • 如果我有 ABCEYHABC,所以函数找到第一个 abc 并运行到下一个 ABC,输出将是:2.

标签: c++ string count contains


【解决方案1】:

如果您使用的是 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());

}

Demo

【讨论】:

  • 请提供一些解释,因为我以前从未见过sregex_iterator,我很确定OP也没有。
  • 我试过你的方法,它的doesnet运行良好......我会发布我的代码让你了解更多
【解决方案2】:

你可以使用 std::string::find。

#include <string>
using namespace std;

size_t count (const string & src, const string & str) {
    size_t cnt = 0, fnd = 0;
    while (fnd = (src.find(str, fnd)) != string::npos) {
        cnt++; fnd++;
    }
    return cnt;
}

...

count("Hello, world!", "ll");

【讨论】:

  • 我没有很好地理解你的代码,但我对构建函数不感兴趣 - 我想为此使用内置函数。
【解决方案3】:

正如paxbun 所说,这里使用了 string::find 方法,这是 string 类的内置函数。

.find()方法的引用string::find ~ C++ Reference

作为另一种方法,包括与您上面的代码对应的类:

#include <iostream>
#include <string>

using namespace std;

//class declaration
class Nucleus{
private:
 string _DNA_strand{"ABCADDASDASABCAFGDACCACABCDA"};

public:
    const string get_codon(){return _DNA_strand;} //accessor of private variable
    unsigned int get_num_of_codon_appearances(const string& _DNA_strand, const string& ) const;
};

//Function  to return the number of times a string is found within another string.
unsigned int Nucleus::get_num_of_codon_appearances(const string& codon, const string& c) const
{
    unsigned int count = 0; //sets count
    size_t counter = 0; //sets counter
    while (counter != string::npos) // if counter does not equal string no position
    {
            size_t i = counter + c.length(); // sets i to counter + length of searched for object
            counter = codon.find(c, i); // .find() method 
            count++;
    }
    return count;
}

//Main Function
int main()
{
    Nucleus temp; //sets the object temp of the class Nucleus
    const string codon = temp.get_codon();
    const string c = "ABC";

    cout << "The Number of times " << c << " is found in " 
    << temp.get_codon() << " is: " << temp.get_num_of_codon_appearances(codon, c) << endl;

    return 0;   
}

示例输出:

The Number of times ABC is found in ABCADDASDASABCAFGDACCACABCDA is: 3

DEMO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-06
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    相关资源
    最近更新 更多