### Transform Algorithm Not Working

Team,
I have a question about the transform() algorithm. Below is a simple program takes a word and determines whether it is a palindrome (a word that is spelled the same way backwards an forwards like "madam").

The problem is with line 51. For some reason when I use the transform() algorithm, I get an error suggesting that the program can't deduce the parameter for the unary operator (in this case, the tolower() function from the cctype library). I don't understand why that is the case since I am using begin(), and
end() iterators for the string correctly (I think?).

I settled on a work around with a simple for loop, but this transform thing is
bugging me since I'm trying to learn the STL. Any insight where I am going wrong? Note: I commented transform() out since it wasn't working.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include #include #include using namespace std; bool palindrome(string& str); int main() { cout << "Please enter a string :" << endl; string someWord; while (cin >> someWord && someWord != "q") { if(palindrome(someWord)) { cout << "The word \"" << someWord << "\" is" " a palindrome!\n"; cout << endl; break; } else { cout << "The word \"" << someWord << "\" is not" " a palindrome!\n"; cout << endl; cout << "Please enter another word :\n"; } } cout << "Bye!" << endl; return 0; } bool palindrome(string& str) { string tempWord; string::iterator indices; int index = 0; for (indices = str.begin(); indices != str.end(); indices++) { if(isalpha(*indices)) tempWord.push_back(*indices); } for (unsigned int i = 0; i < tempWord.size(); i++) tempWord[i] = tolower(tempWord[i]); //transform(tempWord.begin(), tempWord.end(), tempWord.begin(), tolower); string wordChecker(tempWord.rbegin(), tempWord.rend()); if (wordChecker == tempWord) return true; else return false; }``````
Last edited on
There is more than one tolower in the standard library. Even though you're not explicitly including the header that declares the other one (`<locale>`), standard headers are allowed to include each other arbitrarily.

You will have to learn to disambiguate between overload sets, for example using a cast:
 ``12`` ``````transform(tempWord.begin(), tempWord.end(), tempWord.begin(), static_cast(tolower));``````

or using a functor:
 ``12`` ``````transform(tempWord.begin(), tempWord.end(), tempWord.begin(), [](char c){ return tolower(c); });``````

you could also use `boost::algorithm::to_lower(tempWord);`
Last edited on
Thanks, Cubbi!

Good stuff. I have a lot to learn still. I've never used a static_cast with a pointer to function before...let alone use it to disambiguate a function. (sigh).

Thanks alot.
Topic archived. No new replies allowed.