Advertisement
Guest User

9

a guest
Jun 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include<iostream>
  2. #include<array>
  3. #include<algorithm>
  4.  
  5. template <class T, class U>
  6. std::pair<U, T> My_Swap(T a, U b) {
  7. return std::make_pair(b, a);
  8. }
  9.  
  10. template <class T, size_t N>
  11. class MyClass {
  12. T arr[N];
  13. public:
  14. MyClass() {
  15. for (int i = 0; i < N; i++)
  16. arr[i] = rand() % 10;
  17. }
  18. T* get_arr() { return arr; }
  19. };
  20.  
  21.  
  22. void main() {
  23. int a = 5;
  24. double b = 1.2;
  25. std::cout << "\n Before swap:\n " << a << " " << b << std::endl;
  26.  
  27. auto temp = My_Swap(a, b);
  28. auto new_a = temp.first;
  29. auto new_b = temp.second;
  30. std::cout << "\n After swap:\n " << new_a << " " << new_b << std::endl;
  31.  
  32. MyClass<int, 4> c, d;
  33. std::cout << "\n Before swap:\n";
  34. for (int i = 0; i < 4; i++)
  35. std::cout << " " << c.get_arr()[i] << " " << d.get_arr()[i] << std::endl;
  36.  
  37. auto temp_2 = My_Swap(c, d);
  38. c = temp_2.first;
  39. d = temp_2.second;
  40. std::cout << "\n After swap:\n";
  41. for (int i = 0; i < 4; i++)
  42. std::cout << " " << c.get_arr()[i] << " " << d.get_arr()[i] << std::endl;
  43. system("pause");
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement