Re: std::transform for std::map
On Mar 16, 3:34 pm, Ralf Goertz
<r_goe...@expires-2006-11-30.arcornews.de> wrote:
it doesn't seem to be possible to use transform for maps.
That's because maps aren't really containers, in the usual
sense.
#include <algorithm>
#include <map>
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
int doit(int w) {
return w+1;
}
int main() {
map<int,int> m;
vector<int> v;
m[0]=42;
v.push_back(42);
//transform(m.begin(),m.end(),m.begin(),doit); (*)
transform(v.begin(),v.end(),v.begin(),doit);
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}
I understand syntactically why the line (*) does not compile.
But conceptually is doesn't make much sense (IMHO) to not be
able to use maps in std::tranform. Even changing doit to
pair<int, int> doit(pair<int, int> w) {
pair<int,int> result(w);
w.second++;
return result;
}
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.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34