加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 将数组作为子字符串查找到另一个数组中

发布时间:2020-12-16 10:02:26 所属栏目:百科 来源:网络整理
导读:我想知道我是否可以在另一个数组中找到一个数组{67,55,65} {23,45,67,65,66,76,78}.我不想找到数组的单个元素,但整个数组.我尝试了一些代码 #include iostream#include algorithm#include arrayint main(){ std::arrayint,8 in = {23,78}; std::arrayint,3 s
我想知道我是否可以在另一个数组中找到一个数组{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万.我想要一种有效的搜索技术.

我不确定我的代码是否应该有效.我犯了很多错误

bm.cpp:12:20: error: ‘make_boyer_moore_searcher’ is not a member of ‘std’
std::make_boyer_moore_searcher(
^
bm.cpp:15:19: error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’
std::cout << “The string ” << re << ” found at offset ”
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from bm.cpp:1:
/usr/include/c++/4.8/ostream:602:5: error: initializing argument 1 of ‘std::basic_ostream<_CharT,_Traits>& std::operator<<(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: error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’
std::cout << “The string ” << re << ” not foundn”;
^
In file included from /usr/include/c++/4.8/iostream:39:0,const _Tp& __x)
^

解决方法

如果要使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读