Mr.Wang from Next Door
Mr.Wang from Next Door

Reputation: 14820

How to use boyer_moore_search to search char * string in memory?

char * pStart = ...;
char * pLast = ...;
std::string pattern("wn3901s");

std::string::const_iterator it = boost::algorithm::boyer_moore_search<>(  
    ???, 
    ???,  
    pattern.begin(),  
    pattern.end()
);  

I am trying to search a big char* string in memory, pStart points to its first charactor address; pLast points to the last.

However, I don't want to convert the char* string into std::string, because in that case the memory is copied and that is what I want to avoid.

Now I have the problem to pass the first 2 parameters for boyer_moore_search method, which accepts const_iterator there.

Should I add a new inherited class from const_iterator to emulate the char* string? Could there be any example?

Thank you

Upvotes: 2

Views: 405

Answers (2)

sliser
sliser

Reputation: 1645

You can use char * as iterator

char *iterator_ = boost::algorithm::boyer_moore_search(pStart, pLast, pattern.begin(), pattern.end());

An iterator is any object that, pointing to some element in a range of elements (such as an array or a container), has the ability to iterate through the elements of that range using a set of operators (with at least the increment (++) and dereference (*) operators).

The most obvious form of iterator is a pointer.

http://www.cplusplus.com/reference/iterator/

Upvotes: 3

user3127029
user3127029

Reputation: 131

Exactly, as sliser has just pointed out...

#include<boost/algorithm/searching/boyer_moore.hpp>
#include<cstdio>
#include<cstring>

using boost::algorithm::boyer_moore_search;

int main(int argc, char ** argv) {

  const char * text_start = "This is the string in which we will search for the pattern.";
  const char * text_end = text_start + strlen(text_start);
  const char * pattern_start = "search";
  const char * pattern_end = pattern_start + strlen(pattern_start);

  const char * found = boyer_moore_search(text_start, text_end, pattern_start, pattern_end);

  printf("%s", found);

  return 0;
}

Upvotes: 2

Related Questions