shushus

Спринт 7. Указатели. Открываем доступ к памяти. Задание 1.

Jan 12th, 2023
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <cassert>
  2. #include <string>
  3. #include <utility>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T>
  8. void Swap(T* value1, T* value2) {
  9.     std::swap(*value1, *value2);
  10. }
  11.  
  12. int main() {
  13.     using namespace std;
  14.  
  15.     {
  16.         int x = 1;
  17.         int y = 5;
  18.  
  19.         Swap(&x, &y);
  20.         assert(x == 5);
  21.         assert(y == 1);
  22.     }
  23.  
  24.     {
  25.         string one = "one"s;
  26.         string two = "two"s;
  27.         Swap(&one, &two);
  28.         assert(one == "two"s);
  29.         assert(two == "one"s);
  30.     }
  31.  
  32.     {
  33.         struct Point {
  34.             int x, y;
  35.         };
  36.  
  37.         Point p1{1, 2};
  38.         Point p2{3, 4};
  39.  
  40.         Swap(&p1, &p2);
  41.         assert(p1.x == 3 && p1.y == 4);
  42.         assert(p2.x == 1 && p2.y == 2);
  43.     }
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment