将一个数组作为子字符串查找到另一个数组中
Finding an array as a substring into another array
我想知道我是否可以找到一个数组{67,55,65}
到另一个数组{23,45,67,55,65,66,76,78}
。我对找到数组的单个元素不感兴趣,而是对整个数组感兴趣。我尝试了一些代码
#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 foundn";
}
编辑:使用make_boyer_moore_searcher
的原因是我的数组大小,粗略计算,可能是大约 1000 万。我想要一种有效的搜索技术。
我不确定我的代码是否应该工作。我收到很多错误
BM.cpp:12:20: 错误:"make_boyer_moore_searcher"不是"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:错误:初始化参数 1 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&( [_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 <<" 未找到"; ^ 在/usr/include/c++/4.8/iostream:39:0 包含的文件中, 从 BM.cpp:1: /usr/include/c++/4.8/ostream:602:5:错误:初始化参数 1 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&( [_CharT = char; _Traits = std::char_traits; _Tp = std::array]' operator<<(basic_ostream<_CharT, _Traits>&__os, const _Tp& __x( ^
使用make_boyer_moore_searcher
则应包含正确的标头,如参考页中所述:
#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<<
打印出int
的std::array
(您称之为字符串(。您可以重载它或使用循环:
for ( int i : sstr ) {
std::cout << i << ' ';
}
使用您的数据,您应该获得:
The string 67 55 65 found at offset 2
删除make_boyer_moore_searcher
并使用 std::search
。测试它
#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 foundn";
}
编辑:
作为对评论的回应,也可以搜索 2D 数组。在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};
...
测试它
- 使用 jsoncpp 解析 json 数组字符串
- C++ 对象数组字符串输入在控制台上不起作用
- 插入了 C++ 数组字符串数据,但在显示输出时不显示
- 无法区分 JSON 对象/数组/字符串
- 为什么数组(字符串类型)的大小是 24 字节,带有单个空格元素
- 如何使用数组字符串进行输入验证
- (C++) 打印字符数组字符串的单词时出现问题
- 使用Back_inserter从任何位置的输入字符中使用Back_inserter进行过滤数组字符串
- C 函数反转字符数组字符串
- 如何在 c++ 中创建数组(字符串 [] )的向量
- 如何返回数组字符串的指针
- char数组字符串混淆
- 如何从字符数组字符串中提取空格
- 是否可以将 Bitset<8> 的值复制到数组字符串而不转换它们?
- C++数组字符串函数
- 如何在函数中操作字符数组(字符串)的指针,该指针在C / C ++中作为参数传递
- visual将音频文件存储到数组/字符串流C++中
- c++在char数组(字符串)上将what替换为
- 将数组字符串转换为intger c++(初学者)
- 需要设置数组字符串长度,使其恰好为13