J
Jason Heyes
What is a good way of removing elements from std::vector so that the
elements removed satisfy a predicate and end up stored in another
std::vector. It seems as though the algorithm std::remove_if only achieves
half the job. Here is how I would use std::remove_if to remove elements from
std::vector based on predicate:
v.erase(std::remove_if(v.begin(), v.end(), pred), v.end());
After that line is executed I cannot get back the elements that were
removed. So I try to add something before the line:
using namespace std;
remove_copy_if(v.begin(), v.end(), back_inserter(r), not1(pred));
v.erase(remove_if(v.begin(), v.end(), pred), v.end());
This code does the job but it requires twice as many calls to the predicate.
Anyone care to show me an improvement on this? Thanks.
elements removed satisfy a predicate and end up stored in another
std::vector. It seems as though the algorithm std::remove_if only achieves
half the job. Here is how I would use std::remove_if to remove elements from
std::vector based on predicate:
v.erase(std::remove_if(v.begin(), v.end(), pred), v.end());
After that line is executed I cannot get back the elements that were
removed. So I try to add something before the line:
using namespace std;
remove_copy_if(v.begin(), v.end(), back_inserter(r), not1(pred));
v.erase(remove_if(v.begin(), v.end(), pred), v.end());
This code does the job but it requires twice as many calls to the predicate.
Anyone care to show me an improvement on this? Thanks.