I would like to know if I can find an array {67,55,65}
into another array {23,45,67,55,65,66,76,78}
. I am not interested to find individual elements of array, but the array as a whole. I tried some code
#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: The reason to go with make_boyer_moore_searcher
is the size of my array, roughly calculating, may be some 10 million. I want an efficient search technique.
I am not sure if my code is supposed to work. I got many errors
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 found\n"; ^ 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) ^
std::experimental
notstd::
. Besides you should implement operator<< for array<int> or use a loop to print out the values ;) – Bob__make_boyer_moore_searcher
is very fast. I am not sure what logicsearch
use. – AwaitedOnestd::search
? It's only recommended to use specialisations if a need is proven. Also, your proposed alternative isexperimental
only and thus is not guaranteed to exist in all implementations, which has been proven below for yours. – underscore_d