c – 将数组作为子字符串查找到另一个数组中
我想知道我是否可以在另一个数组中找到一个数组{67,55,65} {23,45,67,65,66,76,78}.我不想找到数组的单个元素,但整个数组.我尝试了一些代码
#include <iostream> #include <algorithm> #include <array> int main() { std::array<int,8> in = {23,78}; std::array<int,3> sstr = {67,65}; auto it = std::search(in.begin(),in.end(),std::make_boyer_moore_searcher( sstr.begin(),sstr.end())); if(it != in.end()) std::cout << "The string " << sstr << " found at offset " << it - in.begin() << 'n'; else std::cout << "The string " << sstr << " not foundn"; } 编辑:使用make_boyer_moore_searcher的原因是我的数组的大小,粗略计算,可能是大约1000万.我想要一种有效的搜索技术. 我不确定我的代码是否应该有效.我犯了很多错误
解决方法
如果要使用make_boyer_moore_searcher,则应包含正确的标题,如
reference page中所述:
#include <experimental/algorithm> #include <experimental/functional> 然后,因为那些不属于std,你应该使用: auto it = std::experimental::search(in.begin(),std::experimental::make_boyer_moore_searcher( sstr.begin(),sstr.end())); 在您的代码中,您也尝试使用operator<<打印出一个std :: array of int(你称之为字符串).您可以重载它或使用循环代替: for ( int i : sstr ) { std::cout << i << ' '; } 根据您的数据,您应该获得: The string 67 55 65 found at offset 2 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |