Sunday, November 6, 2016

Gather since cannot slide into middle

1. Gather is to split at gather point and do two partitions.
2. partition put true in the front. So gather front partition needs not1
3. after partition, iter point to last_true+1. So two for-loop +- differs
4. not1 does work direction on lambda needs function
5. when gather point outside [be], it is a pure partition.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>

using namespace std;

template <class RI, typename Func>
auto gather(RI b, RI e, RI p, Func f)->pair<RI, RI>
{
 if (b < p && p < e)
 {
  return{ stable_partition(b, p, not1(function<bool(int i)>(f))),
   stable_partition(p, e, f) };
 }
};

int main()
{
 vector<int> v{ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14 };
 vector<int>::iterator p = v.begin() + 7;
 auto g=gather(v.begin(), v.end(), p, [](int i) {return i%2 == 0; });
 
 for (; g.first != p; ++g.first)
  cout << *g.first << endl;
 cout << "two parti met point" << endl;
 for(; g.second!=p;)
  cout << *--g.second << endl;

 std::string s;
 getline(cin, s);
    return 0;
}


No comments:

Post a Comment