Advertisement
dimon-torchila

Untitled

Feb 16th, 2022
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<map>
  4. #include<utility>
  5.  
  6. using namespace std;
  7.  
  8. template<typename T>
  9.  
  10. T Sqr(const T& x) {
  11.     return x * x;
  12. }
  13.  
  14. template<typename Key, typename Value>
  15. map<Key, Value> operator* (const map<Key, Value>& lhs, const map<Key, Value>& rhs) {
  16.     map<Key, Value> result;
  17.     if (lhs.size() != rhs.size())
  18.         throw exception();
  19.     else
  20.         for (const auto& [k, v] : lhs)
  21.             result[k] = v* v;
  22.     return result;
  23. }
  24.  
  25. template<typename First,typename Second>
  26. pair<First, Second> operator* (const pair<First, Second>& lhs, const pair<First, Second>& rhs) {
  27.     return make_pair(lhs.first * rhs.first, lhs.second * rhs.second);
  28. }
  29.  
  30. template<typename T>
  31. vector<T> operator* (const vector<T>& lhs, const vector<T>& rhs) {
  32.     if (lhs.size() != rhs.size())
  33.         throw exception();
  34.     vector<T> result;
  35.     result.reserve(lhs.size());
  36.     for (size_t i = 0; i < lhs.size(); ++i)
  37.         result.push_back(lhs[i] * rhs[i]);
  38.     return result;
  39. }
  40.  
  41. int main() {
  42.     vector<int> v = { 1, 2, 3 };
  43.     cout << "vector:";
  44.     for (int x : Sqr(v)) {
  45.         cout << ' ' << x;
  46.     }
  47.     cout << endl;
  48.  
  49.     map<int, pair<int, int>> map_of_pairs = {
  50.       {4, {2, 2}},
  51.       {7, {4, 3}}
  52.     };
  53.     cout << "map of pairs:" << endl;
  54.     for (const auto& x : Sqr(map_of_pairs)) {
  55.         cout << x.first << ' ' << x.second.first << ' ' << x.second.second << endl;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement