dimanche 6 décembre 2020

Why does the toupper() function not work in a vector?

I am doing an exercise where I convert all the strings in a vector to uppercase. The limitations for this exercise is to use the toupper() function and a range for-loop. The code is as follows:

for (auto &temp : words)
{
    temp = toupper (words);
}

This does not work. A solution was proposed on this forum (almost 7 years ago from now) where the code was this (the logic behind it was not explained) :

for (auto &i : words)      
    for (auto &j : i)      
        j = toupper(j);

This works oddly. I want to know the logic behind it as it does not make sense to me. My intended code was supposed to use the temp reference to access all the strings in vector and then uppercase them and yet the compiler throws some obscure compiler error like "no matching function call". While the code which works has 2 range for loops using a reference to access the vector and it somehow magically works.

Can somebody explain me this?

Aucun commentaire:

Enregistrer un commentaire