fork(2) download
  1. #include <iostream>
  2. #include <array>
  3. #include <algorithm>
  4. #include <iterator>
  5. using namespace std;
  6.  
  7. using table = array<int, 3>;
  8.  
  9. table sorted(const table &input){
  10. int a = input[0], b = input[1], c = input[2];
  11. if(a > b) swap(a, b);
  12. if(a > c) swap(a, c);
  13. if(b > c) swap(b, c);
  14.  
  15. return {a, b, c};
  16. }
  17.  
  18. template <class T, std::size_t N>
  19. ostream& operator<<(ostream& o, const array<T, N>& arr){
  20. copy(arr.cbegin(), arr.cend(), ostream_iterator<T>(o, " "));
  21. return o;
  22. }
  23.  
  24. template<typename T1, typename T2>
  25. void assert_equal(const T1 &lhs, const T2 &rhs){
  26. if(!(lhs == rhs)){
  27. cout << "Assertion failed!\n";
  28. cout << "lhs: " << lhs << ", rhs: " << rhs << endl;
  29. }
  30. }
  31.  
  32. int main() {
  33. table expected = {1, 2, 3};
  34.  
  35. table arg = expected;
  36. do{
  37. cout << "( " << arg << ")\n";
  38. assert_equal(sorted(arg), expected);
  39. }while(next_permutation(begin(arg), end(arg)));
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
( 1 2 3 )
( 1 3 2 )
( 2 1 3 )
( 2 3 1 )
( 3 1 2 )
( 3 2 1 )
Ok!