>I am trying to learn the STL algorithms so that's why I am doing this
Then it would be a good idea to mention that the operation passed to std::for_each isn't assigned back to each element of the container. std::for_each is more like this:
Code:
template <typename In, typename Op>
Op for_each ( In first, In last, Op op )
{
while ( first != last )
op ( *first++ );
return op;
}
What you want is std::transform, which is similar to std::for_each and actually assigns the result of the operation somewhere, which can be the input container:
Code:
std::transform ( par.begin(), par.end(), par.begin(), std::toupper );
However, because there may be two versions of std::toupper depending on the headers you include, this is actually illegal because std::transform doesn't know which std::toupper to call. This problem isn't unique to std::transform. For example, if in the code you gave, you included both <cctype> and <iostream>, you've managed to include both a single argument version of std::toupper and a two argument version that accepts a locale as the second parameter. The safest fix is to create your own function (or better yet, function object) that picks the right version for you:
Code:
class upper {
public:
int operator() ( int c ) const { return std::toupper ( c ); }
};
std::transform ( par.begin(), par.end(), par.begin(), upper() );