fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4.  
  5. template <typename iter_type>
  6. void print(iter_type beg, iter_type end)
  7. {
  8. std::cout << *beg;
  9.  
  10. while (++beg != end)
  11. std::cout << ' ' << *beg;
  12.  
  13. std::cout << '\n';
  14. }
  15.  
  16.  
  17. int main()
  18. {
  19. int num[] = { 4, -2, -8, 1, 7, 9, -23, -6, 56, 23 };
  20.  
  21. std::cout << "before partition\n\t";
  22. print(std::begin(num), std::end(num));
  23.  
  24. auto p = std::partition(std::begin(num), std::end(num), [](int n) {return n < 0; });
  25.  
  26. std::cout << "\nafter partition\n\t";
  27. print(std::begin(num), std::end(num));
  28.  
  29. std::cout << "\nnegative numbers (" << p - std::begin(num) << ")\n\t";
  30. print(std::begin(num), p);
  31.  
  32. std::cout << "\npositive numbers (" << std::end(num) - p << ")\n\t";
  33. print(p, std::end(num));
  34.  
  35. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
before partition
	4 -2 -8 1 7 9 -23 -6 56 23

after partition
	-6 -2 -8 -23 7 9 1 4 56 23

negative numbers (4)
	-6 -2 -8 -23

positive numbers (6)
	7 9 1 4 56 23