Vserosbuybuy

C++ base guide 7 (templates)

Apr 10th, 2021
917
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×