Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::ifind_first with std::string objects

I am trying to use boost string algorithms for case insensitive search.
total newbie here.

if I am using it this way, I get an error.

std::string str1("Hello world");
std::string str2("hello");
if ( boost::ifind_first(str1, str2) ) some code;

Converting to char pointers resolves the problem.

boost::ifind_first( (char*)str1.c_str(), (char*)str2.c_str() );

Is there a way to search std::string objects directly?

Also, maybe there is another way to know if string is present inside another string with case-insensitive search?

like image 809
Andrew Avatar asked Aug 10 '09 16:08

Andrew


2 Answers

You need to use boost::iterator_range. This works:

  typedef const boost::iterator_range<std::string::const_iterator> StringRange;
  std::string str1("Hello world");
  std::string str2("hello");

  if ( boost::ifind_first(
          StringRange(str1.begin(), str1.end()),
          StringRange(str2.begin(), str2.end()) ) )
      std::cout << "Found!" << std::endl;

EDIT: Using a const iterator_range in the typedef allows passing a temporary range.

like image 191
Fred Larson Avatar answered Nov 17 '22 12:11

Fred Larson


The more simplicity way

std::string str1("Hello world");
std::string str2("hello");
if (boost::icontains(str1, str2)) {
   ...
}
like image 1
Like Avatar answered Nov 17 '22 11:11

Like