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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>

using namespace std;


bool palindrome(string& str);

int main()
{
    cout << "Please enter a string <q to quit>:" << 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 <q to quit>:\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:
1
2
transform(tempWord.begin(), tempWord.end(), tempWord.begin(),
                    static_cast<int(*)(int)>(tolower));

or using a functor:
1
2
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.