separate even values from the odd ones - even comes first : partition « STL Algorithms Modifying sequence operations « C++






separate even values from the odd ones - even comes first

  
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
bool IsEven (const int& nNumber){
    return ((nNumber % 2) == 0);
}

int main ()
{
    vector <int> v;

    for (int nNum = 0; nNum < 10; ++ nNum)
        v.push_back (nNum);

    vector <int> vecCopy (v);

    // separate even values from the odd ones - even comes first.
    partition (v.begin (), v.end (), IsEven);

    for (size_t nItem = 0;  nItem < v.size (); ++ nItem)
        cout << v [nItem] << ' ';

    return 0;
}
  
    
  








Related examples in the same category

1.Use stable_partition and partition