R
Ralf Goertz
I try tor remove elements from a std::map<int,some_class> for which a
condition depending on the value is met. For containers like vector this
is easy
#include <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;
typedef map<int,int> intmap;
bool is42_m(intmap::value_type w) {
return w.second==42;
}
bool is42_v(const int& w) {
return w==42;
}
int main() {
map<int,int> m;
m[14711]=52;
m[17]=42;
m[4]=17;
vector<int> v;
v.push_back(52);
v.push_back(42);
v.push_back(17);
v.erase(remove_if(v.begin(),v.end(),is42_v),v.end());
//v.erase(remove_if(m.begin(),m.end(),is42_m),m.end());
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}
I know that this can't work with a map since remove_if rearranges the
elements of the container which is not possible for a map. But how can I
do that with a map?
Ralf
condition depending on the value is met. For containers like vector this
is easy
#include <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;
typedef map<int,int> intmap;
bool is42_m(intmap::value_type w) {
return w.second==42;
}
bool is42_v(const int& w) {
return w==42;
}
int main() {
map<int,int> m;
m[14711]=52;
m[17]=42;
m[4]=17;
vector<int> v;
v.push_back(52);
v.push_back(42);
v.push_back(17);
v.erase(remove_if(v.begin(),v.end(),is42_v),v.end());
//v.erase(remove_if(m.begin(),m.end(),is42_m),m.end());
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}
I know that this can't work with a map since remove_if rearranges the
elements of the container which is not possible for a map. But how can I
do that with a map?
Ralf