我想知道我是否可以在另一个数组_683148中找到一个数组 {67,55,65}
. 我不想找到数组的单个元素,但整个数组 . 我尝试了一些代码
#include <iostream>
#include <algorithm>
#include <array>
int main()
{
std::array<int,8> in = {23,45,67,55,65,66,76,78};
std::array<int,3> sstr = {67,55,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 found\n";
}
Edit: 使用 make_boyer_moore_searcher
的原因是我的数组的大小,粗略计算,可能是大约1000万 . 我想要一种有效的搜索技术 .
我不确定我的代码是否应该有用 . 我收到很多错误
bm.cpp:12:20:错误:'make_boyer_moore_searcher'不是'std'std :: make_boyer_moore_searcher的成员(^ bm.cpp:15:19:错误:无法将'std :: basic_ostream'左值绑定到'std :: basic_ostream &&'std :: cout <<“字符串”<< re <<“在偏移量”^包含在/ usr / include / c /4.8/iostream:39:0中的文件中找到,来自bm.cpp:1 :/ usr / include / c /4.8/ostream:602:5:错误:初始化'std :: basic_ostream <_CharT,_Traits>&std :: operator <<的参数1(std :: basic_ostream <_CharT,_Traits> && ,const _Tp&)[with _CharT = char; _Traits = std :: char_traits; _Tp = std :: array]'operator <<(basic_ostream <_CharT,_Traits> && __os,const _Tp&__x)^ bm.cpp:18:19 :错误:无法将'std :: basic_ostream'左值绑定到'std :: basic_ostream &&'std :: cout <<“未找到字符串”<< re <<“\ n”; ^包含在/ usr / include中的文件/ c /4.8/iostream:39:0,来自bm.cpp:1:/ usr / include / c /4.8/ostream:602:5:错误:初始化'std :: basic_ostream <_CharT,_Traits>&的参数1的std :: OPER ator <<(std :: basic_ostream <_CharT,_Traits> &&,const _Tp&)[with _CharT = char; _Traits = std :: char_traits; _Tp = std :: array]'operator <<(basic_ostream <_CharT,_Traits> && _os,const Tp& x)^
2 回答
如果你想使用
make_boyer_moore_searcher
,你应该包括正确的 Headers ,如_683153中所述:然后,因为那些不属于
std
,你应该使用以下方法调用它们:在您的代码中,您也尝试使用
operator<<
打印std::array
的int
(您调用字符串) . 您可以重载它或使用循环代替:根据您的数据,您应该获得:
删除
make_boyer_moore_searcher
并仅使用std::search
. Test itEdit :
在回应评论时,还可以搜索2d阵列 . 在std::search中使用
operator==
比较元素 . 因此,在这种情况下,您可以通过将代码更改为:Test it