Advertisement
RobertDeMilo

YB1.7 Введение в шаблоны

Oct 27th, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <sstream>
  5. #include <utility>
  6.  
  7. using namespace std;
  8.  
  9. //int Sqr(int x)
  10. //{
  11. //  return x * x;
  12. //}
  13.  
  14. //double Sqr(double x)
  15. //{
  16. //  return x * x;
  17. //}
  18.  
  19. // Шаблонный оператор умножения для пары любого типа
  20. template<typename First, typename Second>
  21. pair<First, Second> operator*(const pair<First, Second>& p1, const pair<First, Second>& p2)
  22. {
  23.     First f = p1.first * p2.first;
  24.     Second s = p1.second * p2.second;
  25.  
  26.     return make_pair(f, s);
  27. }
  28.  
  29. template <typename T>
  30. T Sqr(T x)
  31. {
  32.     return x * x;
  33. }
  34.  
  35. int main()
  36. {
  37.     /*cout << Sqr(2) << endl;
  38.     cout << Sqr(2.5) << endl;*/
  39.  
  40.     auto p = make_pair(2.5, 3);
  41.     auto res = Sqr(p);
  42.  
  43.     cout << res.first << endl;
  44.     cout << res.second << endl;
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement