views:

259

answers:

4

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?

A: 

What error do you get?

navigator
it fails to compile detail::first_finderFCould not find a match for 'detail::first_finderF<const char *,is_iequal>::first_finderF(undefined,is_iequal)'
Andrew
A: 
Glen
A: 

(char*)str.c_str() is actually performing a const_cast: const_cast<char*>(str.c_str()). I very seriously doubt that it is necessary to cast away const in order to search through the string.

I have never used boost::ifind_first, but according to the documentation, the function takes two ranges. I suppose there's a way to create a range from a string? OTOH, I'd wonder if a string wasn't a perfect range.

It might be helpful if you post the full error messages of the compiler you used.

sbi
+4  A: 

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.

Fred Larson