C++ Tutorial/STL Algorithms Modifying sequence operations/partition

Материал из C\C++ эксперт
Перейти к: навигация, поиск

use stable_partition on the vecCopy - maintains relative order

<source lang="cpp">#include <algorithm>

  1. include <vector>
  2. 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);
   // a copy of the sample vector
   vector <int> vecCopy (v);
   // use stable_partition on the vecCopy - maintains relative order
   stable_partition (vecCopy.begin (), vecCopy.end (), IsEven);
   for (size_t nItem = 0; nItem < vecCopy.size (); ++ nItem)
       cout << vecCopy [nItem] << " ";
   return 0;

}</source>