JDiMatteo
JDiMatteo

Reputation: 13130

boost::algorithm::boyer_moore_search OO example

Please help me get a basic example of the object oriented boost::algorithm::boyer_moore_search interface working.

The procedural interface works for me, e.g this compiles and prints "pattern found":

#include <iostream>
#include <string>
#include <boost/algorithm/searching/boyer_moore.hpp>

int main() {
    std::string corpus("hello world");
    std::string pattern("hello");

    if (corpus.end() != boost::algorithm::boyer_moore_search(corpus.begin(), corpus.end(),
                                                             pattern.begin(), pattern.end()))
    {
        std::cout << "pattern found" << std::endl;
    }
    return 0;
}

But the following using the object oriented interface gives a compiler error, e.g.

#include <iostream>
#include <string>
#include <boost/algorithm/searching/boyer_moore.hpp>

int main() {
    std::string corpus("hello world");
    std::string pattern("hello");

    boost::algorithm::boyer_moore<std::string::const_iterator, std::string::const_iterator>
        search(pattern.begin(), pattern.end());

    if (corpus.end() != search(corpus.begin(), corpus.end()))
    {
        std::cout << "pattern found" << std::endl;
    }
    return 0;
}

This gives the following error, which is easily reproducible on ideone:

$ g++ test2.cpp 
In file included from test2.cpp:3:0:
/usr/include/boost/algorithm/searching/boyer_moore.hpp: In instantiation of ‘class boost::algorithm::boyer_moore<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > >’:
test2.cpp:10:14:   required from here
/usr/include/boost/algorithm/searching/boyer_moore.hpp:104:39: error: no type named ‘skip_table_t’ in ‘class __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >’
         typename traits::skip_table_t skip_;
                                       ^
/usr/include/boost/algorithm/searching/boyer_moore.hpp: In instantiation of ‘boost::algorithm::boyer_moore<patIter, traits>::boyer_moore(patIter, patIter) [with patIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; traits = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]’:
test2.cpp:10:44:   required from here
/usr/include/boost/algorithm/searching/boyer_moore.hpp:63:50: error: using invalid field ‘boost::algorithm::boyer_moore<patIter, traits>::skip_’
                   suffix_ ( k_pattern_length + 1 )
                                                  ^
/usr/include/boost/algorithm/searching/boyer_moore.hpp: In instantiation of ‘corpusIter boost::algorithm::boyer_moore<patIter, traits>::do_search(corpusIter, corpusIter) const [with corpusIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; patIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; traits = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]’:
/usr/include/boost/algorithm/searching/boyer_moore.hpp:92:66:   required from ‘corpusIter boost::algorithm::boyer_moore<patIter, traits>::operator()(corpusIter, corpusIter) const [with corpusIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; patIter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; traits = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]’
test2.cpp:12:60:   required from here
/usr/include/boost/algorithm/searching/boyer_moore.hpp:133:27: error: using invalid field ‘boost::algorithm::boyer_moore<patIter, traits>::skip_’
                 k = skip_ [ curPos [ j - 1 ]];
                           ^

I'm using gcc 4.8.2 and boost 1.54.

I'm trying to use the OO interface, because I am going to repeatedly search the same pattern in different corpuses and don't want to repeatedly calculate the same skip table.

Upvotes: 1

Views: 1162

Answers (1)

Niall C.
Niall C.

Reputation: 10918

The boyer_moore_search constructor takes a single template type parameter because both arguments to the constructor are the same type. You've provided two.

Change your declaration of search to:

boost::algorithm::boyer_moore<std::string::const_iterator>
    search(pattern.begin(), pattern.end());

and it will work.

Upvotes: 3

Related Questions