Hi!
I would like to split vector elements into the two vectors. One output vector shall contain elements that, for instance, are odd, and another output vector shall contain even elements (actually the case is more complicated;). I wrote something like this:
typedef vector<int> ItemVec;
static bool isOdd(const int &i)
{
return i%2;
}
static bool not_isOdd(const int &i)
{
return !isOdd(i);
}
void split_example(vector& vec)
{
ItemVector oddVec;
std::remove_copy_if(vec.begin(), vec.end(), back_inserter(oddVec), not_isOdd);
vec.erase(std::remove_if(vec.begin(), vec.end(), isOdd), vec.end());
// sort both vec - different sorting methodes
...
///
vec.insert(vec.begin(), oddVec.begin(), oddVec.end());
}
However, all vectors elements are checked 2 times (remove_copy_if and remove_if). Is there a better algorithm providing better performance, or old good for loop shall be applied?
Edit: found similar thread, sorry for inconvenience.