Advertisement
chevengur

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

Oct 27th, 2023
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 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.     Print(out, kunteynir);
  23.     return out;
  24. }
  25.  
  26. template<typename type>
  27. ostream& operator<< (ostream& out, const vector<type>kunteynir){
  28.     Print(out, kunteynir);
  29.     return out;
  30. }
  31.  
  32. int main() {
  33.     const vector<int> ages = {10, 5, 2, 12};
  34.     const set<int> set_ages = {20, 15, 3 , 1};
  35.     cout << ages << endl;
  36.     cout << set_ages << endl;
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement