Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否有一个用于std::search的函数,std::find的std::count是什么?_C++_Stl - Fatal编程技术网

C++ 是否有一个用于std::search的函数,std::find的std::count是什么?

C++ 是否有一个用于std::search的函数,std::find的std::count是什么?,c++,stl,C++,Stl,如果标题听起来很奇怪,这里有另一种解释: 如果我有一个范围a,我想计算另一个范围b在范围a中出现了多少次,是否有一个std::函数来执行此操作 如果没有,是否有一种简单的方法(ofc我可以使用std::search-我说的是更优雅的东西)手动循环?您可以在范围a上使用lambda,在范围B上使用std::find 编辑:将std::search替换为std::find。我认为您需要构建自己的。以下是我想到的一个实现 template <typename Iterator1, typenam

如果标题听起来很奇怪,这里有另一种解释:

如果我有一个范围a,我想计算另一个范围b在范围a中出现了多少次,是否有一个
std::
函数来执行此操作

如果没有,是否有一种简单的方法(ofc我可以使用
std::search
-我说的是更优雅的东西)手动循环?

您可以在范围a上使用lambda,在范围B上使用std::find


编辑:将std::search替换为std::find。

我认为您需要构建自己的。以下是我想到的一个实现

template <typename Iterator1, typename Iterator2>
size_t subsequence_count(Iterator1 haystack_begin, Iterator1 haystack_end, Iterator2 needle_begin, Iterator2 needle_end) {
    size_t count = 0;
    while (true) {
        haystack_begin = std::search(haystack_begin, haystack_end, needle_begin, needle_end);
        if (haystack_begin == haystack_end)
            return count;
        count++;
        haystack_begin++;
    }
}

template <typename Iterator1, typename Iterator2, typename BinaryPredicate>
size_t subsequence_count(Iterator1 haystack_begin, Iterator1 haystack_end, Iterator2 needle_begin, Iterator2 needle_end, BinaryPredicate predicate) {
    size_t count = 0;
    while (true) {
        haystack_begin = std::search(haystack_begin, haystack_end, needle_begin, needle_end, predicate);
        if (haystack_begin == haystack_end)
            return count;
        count++;
        haystack_begin++;
    }
}
模板
大小子序列计数(迭代器1干垛开始,迭代器1干垛结束,迭代器2针开始,迭代器2针结束){
大小\u t计数=0;
while(true){
haystack\u begin=std::search(haystack\u begin、haystack\u end、needle\u begin、needle\u end);
如果(干草堆开始==干草堆结束)
返回计数;
计数++;
干草堆_begin++;
}
}
模板
大小子序列计数(迭代器1 haystack\u begin、迭代器1 haystack\u end、迭代器2 pineel\u begin、迭代器2 pineel\u end、二进制谓词){
大小\u t计数=0;
while(true){
haystack\u begin=std::search(haystack\u begin、haystack\u end、needle\u begin、needle\u end、谓词);
如果(干草堆开始==干草堆结束)
返回计数;
计数++;
干草堆_begin++;
}
}
某些代码使用以下命令:

int main() {
    std::vector<int> haystack = {1, 19, 7, 23, 2, 19, 19, 19, 19};
    std::vector<int> needle   = {19, 19};

    assert(subsequence_count(begin(haystack), end(haystack), begin(needle), end(needle) == 3);
}
intmain(){
向量干草堆={1,19,7,23,2,19,19,19,19};
std::向量针={19,19};
断言(子序列计数(开始(干草堆)、结束(干草堆)、开始(针)、结束(针)=3);
}

如果您想比
O(nm)更有效地执行此操作,对于模式中的<代码> M>代码>字符,在搜索的字符串中,代码代码>,你可以考虑一个<强>后缀树。本质上,这意味着你构建了一个专门的树结构,它同时描述了一个字符串的所有可能后缀。,您的后缀字符串将同时表示“ratatat”、“atatat”、“tatat”、“atat”、“tat”、“at”和“t”。因此,构建树后,您可以很快找到(并计算)特定字符串的所有出现情况。当然,构建树需要一些编程工作和一些内存

这里有一个非常好的描述(这是指斯基纳的书,我在那里读到过关于他们的书)

我开始搜索后缀树C++实现。这里有几个有用的堆栈溢出问题,但我可以告诉你,在STD中没有什么。 编辑以添加替代算法

再想一想,我认为字符串匹配可能是一个更好的解决方案,尤其是因为有一个现成的方法——而且您所说的要做的就是找到一个特定的搜索字符串(如果您想计算不同搜索字符串的出现次数,后缀树是很好的)。基本上,b-m算法所做的是利用搜索字符串中的结构,在出现不匹配时,使用搜索字符串的预计算表(预处理要搜索的字符串的c.f.后缀树)。因此,您应该能够手动循环使用boyer moore boost搜索(而不是使用std搜索)并获得显著的效率提升。

如果有疑问,请使用boost:)

#包括
#包括
#包括
使用名称空间std;
使用名称空间boost;
使用boost::algorithm::find_all;
int main(int argc,char*argv[])
{
病媒草堆{11.0,22.0,33.0,22.0,33.0,44.0,22.0,33.0,22.0};
std::向量针{22.0,33.0};
std::向量输出;
boost::算法::查找所有(out、haystack、pine);

我猜不存在这样的标准函数,因为不清楚它是否应该计算重叠matches@anatolyg是的,实际上这是我帮助一些有抱负的年轻程序员数一数序列中的所有单词…我们讨论的是结果(“aaab”,“aa”)2或1:D@anatolyg…它不是n^2…对于范围内的每个元素,您只能读取另一个范围内的一个元素。@nosensetal您的意思是它真的是
O(nm)
模式中有m个字符的地方?假设是这样,我正在修改我的答案。我猜是这样的,比如aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
我认为模式通常是要搜索的字符串的一小部分,但这相当懒惰,
O(nm)
更好。boter moore不会处理自定义数据。。。该算法分配两个内部表。第一个表与模式的长度成比例;第二个表中“字母表”的每个成员有一个条目。对于(8位)字符类型,该表包含256个条目。因此,对于int表,有4G个条目:D
#include <boost/algorithm/string/finder.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/foreach.hpp>

using namespace std;
using namespace boost;
using boost::algorithm::find_all;

int main(int argc, char* argv[])
{
    std::vector<double>  haystack{11.0,22.0,33.0,22.0,33.0,44.0,22.0,33.0,22.0};
    std::vector<double> needle{22.0,33.0};

    std::vector<boost::iterator_range<std::vector<double>::iterator>> out;
    boost::algorithm::find_all(out, haystack, needle);

    cout << "matches=" << out.size() << endl;
    cout << endl;

}