How to apply transform to an STL map in C++

daj picture daj · Oct 24, 2011 · Viewed 15.5k times · Source

In C++, I'm using transform to change all the values of a map to uppercase.

  std::map<std::string, std::string> data = getData();

  // make all values uppercase
  std::transform(data.begin(), data.end(), data.begin(),
         [](std::pair<std::string, std::string>& p) {
           boost::to_upper(p.second);
           return(p);
         });

This gives me the following compilation error:

/opt/local/include/gcc46/c++/bits/stl_algo.h:4805:2: error: no match for call to '(main(int, char**)::<lambda(std::pair<std::basic_string<char>, std::basic_string<char> >&)>) (std::pair<const std::basic_string<char>, std::basic_string<char> >&)

I think there's something wrong with the type of the argument in my lambda expression. It's probably something simple, but I can't seem to figure out what's expected.

Answer

pmr picture pmr · Oct 24, 2011

You are missing the const in the first type of the pair.

[](std::pair<const std::string, std::string>& p) {

However this is not your problem: You cannot use a map as the OutputIterator, as they do not support assignment. You can, however mutate the second argument using std::for_each.

Good old map_to_foobar:

std::for_each(data.begin(), data.end(), 
              [](std::pair<const std::string, std::string>& p) {
                p.second = "foobar";
              });

Conceptual stuff: Calling transform with the same range as input and output is quite legit and makes a lot of sense if all your functors return by value and don't mutate their arguments. However, mutating something in place can be a faster (or at least look faster in code, nevermind the optimizing compiler) and makes a lot of sense with member functions.