1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
// http://ideone.com/JuwFBe
#include <iostream>
#include <algorithm>
#include <iterator>
template <typename iter_type>
void print(iter_type beg, iter_type end)
{
std::cout << *beg;
while (++beg != end)
std::cout << ' ' << *beg;
std::cout << '\n';
}
int main()
{
int num[] = { 4, -2, -8, 1, 7, 9, -23, -6, 56, 23 };
std::cout << "before partition\n\t";
print(std::begin(num), std::end(num));
// http://en.cppreference.com/w/cpp/algorithm/partition
auto p = std::partition(std::begin(num), std::end(num), [](int n) {return n < 0; });
std::cout << "\nafter partition\n\t";
print(std::begin(num), std::end(num));
std::cout << "\nnegative numbers (" << p - std::begin(num) << ")\n\t";
print(std::begin(num), p);
std::cout << "\npositive numbers (" << std::end(num) - p << ")\n\t";
print(p, std::end(num));
}
|