std::search isn't working!

This is a discussion on std::search isn't working! within the C++ Programming forums, part of the General Programming Boards category; I'm trying to do a case insensitive comparison on two strings. I found this piece of code online but since ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    40

    std::search isn't working!

    I'm trying to do a case insensitive comparison on two strings. I found this piece of code online but since I've never used std::search before, I don't know how to make the predicate part work. Here's what I've got so far...

    Code:
    bool ci_equal(char ch1, char ch2)
    	{
    		return toupper((unsigned char)ch1) == toupper((unsigned char)ch2);
    	}
    	
    	size_t ci_find(const string& str1, const string& str2)
    	{
    		string::const_iterator pos = search(str1.begin( ), str1.end( ), str2.begin( ), str2.end( ), ci_equal());  //PROBLEM IN THIS LINE!
    		if (pos == str1.end( ))
    			return string::npos;
    		else
    			return pos - str1.begin( );
    	}
    It won't compile. Can anyone tell me how to make this code work?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    ci_equal is a function, not a function object, and to pass a pointer to this function, leave out the parenthesis.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by black_spot1984 View Post
    I'm trying to do a case insensitive comparison on two strings. I found this piece of code online but since I've never used std::search before, I don't know how to make the predicate part work. Here's what I've got so far...

    Code:
    bool ci_equal(char ch1, char ch2)
    	{
    		return toupper((unsigned char)ch1) == toupper((unsigned char)ch2);
    	}
    	
    	size_t ci_find(const string& str1, const string& str2)
    	{
    		string::const_iterator pos = search(str1.begin( ), str1.end( ), str2.begin( ), str2.end( ), ci_equal() );  //PROBLEM IN THIS LINE!
    		if (pos == str1.end() )
    			return string::npos;
    		else
    			return pos - str1.begin( );
    	}
    It won't compile. Can anyone tell me how to make this code work?
    Remove the parentheses.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    Thanks for the reply. I changed my code so it looks like:

    Code:
    bool ci_equal(char ch1, char ch2)
    	{
    		return toupper((unsigned char)ch1) == toupper((unsigned char)ch2);
    	}
    	
    	size_t ci_find(const string& str1, const string& str2)
    	{
    		string::const_iterator pos = search(str1.begin( ), str1.end( ), str2.begin( ), str2.end( ), ci_equal);
    		if (pos == str1.end( ))
    			return string::npos;
    		else
    			return pos - str1.begin( );
    	}
    Now I'm getting the error message:

    error: no matching function for call to ‘search(__gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)’
    /usr/lib/gcc/i386-redhat-linux/4.3.0/../../../../include/c++/4.3.0/bits/stl_algo.h:4123: note: candidates are: _FIter1 std::search(_FIter1, _FIter1, _FIter2, _FIter2, _BinaryPredicate) [with _FIter1 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _FIter2 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _BinaryPredicate = bool (webCrawler::*)(char, char)]

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Is ci_equal a member function?

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    For those who are curious, what I was doing wrong was in the way I was including the two files. It worked when I got them both into the file in the right place.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you're getting compile errors by switching around the order of your #include statements, then you've got other problems that need fixing.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 04:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 08:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 05:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21