tranerius

14. Swap переменных

Dec 11th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. template<class T>
  4. void swap(T &a, T &b) {
  5.     T temp = b;
  6.     b = a;
  7.     a = temp;
  8. }
  9. int main() {
  10.     setlocale(LC_ALL, "ru");
  11.     int i_1=10, i_2=20;
  12.     float f_1=0.1, f_2=0.2;
  13.     std::string string_1 = "один", string_2 = "два";
  14.     char c_1 = 'a', c_2 = 'b';
  15.     std::cout << "Первая переменная\t" << i_1 << "\nВторная переменная\t" << i_2 << std::endl << std::endl;
  16.     std::cout << "Первая переменная\t" << f_1 << "\nВторная переменная\t" << f_2 << std::endl << std::endl;
  17.     std::cout << "Первая переменная\t" << string_1 << "\nВторная переменная\t" << string_2 << std::endl << std::endl;
  18.     std::cout << "Первая переменная\t" << c_1 << "\nВторная переменная\t" << c_2 << std::endl << std::endl;
  19.     swap(i_1,i_2);
  20.     swap(f_1, f_2);
  21.     swap(string_1, string_2);
  22.     swap(c_1, c_2);
  23.     std::cout << "Первая переменная\t" << i_1 << "\nВторная переменная\t" << i_2 << std::endl << std::endl;
  24.     std::cout << "Первая переменная\t" << f_1 << "\nВторная переменная\t" << f_2 << std::endl << std::endl;
  25.     std::cout << "Первая переменная\t" << string_1 << "\nВторная переменная\t" << string_2 << std::endl << std::endl;
  26.     std::cout << "Первая переменная\t" << c_1 << "\nВторная переменная\t" << c_2 << std::endl << std::endl;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment