Vserosbuybuy

C++ base guide 7 (templates)

Apr 10th, 2021
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <fstream>
  4. #include <math.h>
  5. #include <vector>
  6. #include <string>
  7. #include <sstream>
  8. #include <map>
  9. #include <set>
  10.  
  11. using namespace std;
  12.  
  13. template<typename First, typename Second>
  14. ostream& operator<<(ostream& out, const pair<First, Second>& p);
  15.  
  16. template<typename Iter>
  17. string print_it(Iter it) {
  18.     stringstream st;
  19.     bool flag = true;
  20.     for (auto& x : it) {
  21.         if (!flag) {
  22.             st << " ";
  23.         }
  24.         flag = false;
  25.         st << x;
  26.     }
  27.  
  28.     string s;
  29.     getline(st, s);
  30.  
  31.     return s;
  32. }
  33.  
  34. template<typename T>
  35. ostream& operator<<(ostream& out, const vector<T>& v) {
  36.     out << "[ " << print_it(v) << " ]";
  37.     return out;
  38. }
  39.  
  40. template<typename First, typename Second>
  41. ostream& operator<<(ostream& out, const map<First, Second>& v) {
  42.     out << "( " << print_it(v) << " )";
  43.  
  44.     return out;
  45. }
  46.  
  47. template<typename First, typename Second>
  48. ostream& operator<<(ostream& out, const pair<First, Second>& p) {
  49.     out << "{ " << p.first << "," << p.second << " }";
  50.     return out;
  51. }
  52.  
  53. template<typename T>
  54. T Min(T a, T b) {
  55.     if (a < b) {
  56.         return a;
  57.     }
  58.     return b;
  59. }
  60.  
  61. template<typename First, typename Second>
  62. struct Pair{
  63.     First first;
  64.     Second second;
  65. };
  66.  
  67. template<typename First, typename Second>
  68. ostream& operator<<(ostream& out, const Pair<First, Second>& p) {
  69.     out << "{ " << p.first << "," << p.second << " }";
  70.     return out;
  71. }
  72.  
  73. int main() {
  74. #ifdef _DEBUG
  75.     freopen("input.txt", "r", stdin);
  76.     freopen("output.txt", "w", stdout);
  77. #endif //
  78.     Pair<int, double> p;
  79.     p.first = 3;
  80.     p.second = 3.54;
  81.     cout << p << endl;
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment