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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)