R
Ralf Goertz
Hi,
it doesn't seem to be possible to use transform for maps.
#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?
Thanks,
Ralf
it doesn't seem to be possible to use transform for maps.
#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?
Thanks,
Ralf