【问题标题】:Finding an array as a substring into another array将数组作为子字符串查找到另一个数组中
【发布时间】:2016-08-10 02:46:13
【问题描述】:

我想知道是否可以在另一个数组{23,45,67,55,65,66,76,78} 中找到一个数组{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";
}

编辑: 选择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 & std::operator&& 的参数 1, const _Tp&) [with _CharT = char; _Traits = std::char_traits; _Tp = std::array]’ 运算符&& __os, const _Tp& __x) ^ bm.cpp:18:19:错误:无法将“std::basic_ostream”左值绑定到“std::basic_ostream&&” std::cout & std::operator&& 的参数 1, const _Tp&) [with _CharT = char; _Traits = std::char_traits; _Tp = std::array]’ 运算符&& __os, const _Tp& __x) ^

【问题讨论】:

  • 你看过那个函数的reference page吗?
  • @Bob__ 感谢您的评论。我看了一下函数,有一个字符串匹配的例子。我不确定这是否适用于数组匹配。可能是我错过了什么。
  • 你应该包含正确的标题并且那些函数属于std::experimental而不是std::。此外,您应该实现 operator 或使用循环打印出值;)
  • @JonathanMee。我无法承受缓慢的搜索,因为我正在搜索的数组太大而且我确信make_boyer_moore_searcher 非常快。我不确定search 使用什么逻辑。
  • @AwaitedOne 您是否对std::search 使用的默认方法进行了基准测试?仅在证明需要时才建议使用专业化。此外,您提出的替代方案仅是 experimental,因此不能保证在所有实现中都存在,这已在下面为您证明。

标签: c++ arrays search


【解决方案1】:

删除 make_boyer_moore_searcher 并仅使用 std::searchTest it

#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(), sstr.begin(), sstr.end());
    if(it != in.end())
        std::cout << "The string found at offset "
                  << it - in.begin() << '\n';
    else
        std::cout << "The string not found\n";
}

编辑

响应评论,也可以搜索二维数组。在std::search 中,使用operator== 比较元素。因此,在这种情况下,您可以通过将代码更改为:

std::array<std::array<int, 3>, 4> in {{ {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} }};
std::array<std::array<int,3>, 1> sstr = {10,11,12};
...

Test it

【讨论】:

  • 它会在二维数组中搜索,比如我有std::array&lt;std::array&lt;int, 3&gt;, 4&gt; in { { { {1, 2, 3, 4} }, { {5, 67, 55, 65} }, { {9, 10, 11, 12} } } };
【解决方案2】:

如果您想使用make_boyer_moore_searcher,您应该包含正确的标题,如reference page 中所述:

#include <experimental/algorithm>
#include <experimental/functional>

那么,由于那些不属于std,你应该使用:

auto it =   std::experimental::search(in.begin(), in.end(),
                 std::experimental::make_boyer_moore_searcher(
                   sstr.begin(), sstr.end()));

在您的代码中,您还尝试使用operator&lt;&lt; 打印出intstd::array(您称之为字符串)。您可以重载它或使用循环代替:

for ( int i : sstr ) {
     std::cout << i << ' ';
}

使用您的数据,您应该获得:

The string 67 55 65  found at offset 2

【讨论】:

  • 尽管这可能很明显,但我要提一下,并非所有标准库实现都实现了所有实验性扩展。
  • bm.cpp:4:34: fatal error: experimental/algorithm: No such file or directory #include &lt;experimental/algorithm&gt; ^ compilation terminated.
  • @AwaitedOne 正如 user2079303 所怀疑的那样。因此,要么使用不同的实现,要么不使用experimental 功能。
  • @AwaitedOne 抱歉,您可以尝试更新您的编译器,但正如其他人所说,这是一个实验性功能。
  • @user2079303 这可能很明显,但记住它总是一个好主意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多