Advertisement
chevengur

СПРИНТ № 2 | Шаблоны функции | Урок 5: Универсальные функции вывода контейнеров 5/6

Oct 27th, 2023
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4.  
  5. using namespace std;
  6.  
  7. template<typename type>
  8. void Print(ostream& out, const type& kunteynir){
  9.     bool first_ap = true;
  10.    
  11.     for(const auto& kun: kunteynir){
  12.         if(!first_ap){
  13.             out << ", ";
  14.         }
  15.         first_ap = false;
  16.         out << kun;  
  17.     }
  18. }
  19.  
  20. template<typename type>
  21. ostream& operator<< (ostream& out, const set<type>kunteynir){
  22.     out << "{";
  23.     Print(out, kunteynir);
  24.     out << "}";
  25.     return out;
  26. }
  27.  
  28. template<typename type>
  29. ostream& operator<< (ostream& out, const vector<type>kunteynir){
  30.     out << "[";
  31.     Print(out, kunteynir);
  32.     out << "]";
  33.     return out;
  34. }
  35.  
  36.  
  37. int main() {
  38.     const vector<int> ages = {10, 5, 2, 12};
  39.     const set<int> set_ages = {20, 15, 3 , 1};
  40.     cout << ages << endl;
  41.     cout << set_ages << endl;
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement