Re: std::transform for std::map
James Kanze wrote:
On Mar 16, 3:34 pm, Ralf Goertz
<r_goe...@expires-2006-11-30.arcornews.de> wrote:
doesn't help. What would be the right way to do it?
Probably using an iterator adaptor or an iterator facade from
Boost. Basically, what you want is an iterator which only
"shows" the second element of the pair.
What about using std::for_each? map<K,V>::value_type is typedef'ed as
std::pair<const K, V>, meaning that as long as you have a non-const map,
the iterator you get from begin() will allow you to change the value. I
think the following does what the OP wants.
#include <iostream>
#include <map>
typedef std::map<int,int> intmap;
void doit( intmap::value_type &item ){
// Modify the second element (value) of the pair
item.second++;
}
int main(int argc, char* argv[])
{
intmap m;
m[6*9] = 42;
m[2] = 4;
m[3] = 9;
std::for_each(m.begin(),m.end(),doit);
// Here m[54] = 43, m[2] = 5, m[3] = 10
return EXIT_SUCCESS;
}