Advertisement
RobertDeMilo

WB3.2 Алгоритмы count и count_if, лямбда функция

Sep 4th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. void Print(const vector <int>& v, const string& title)
  10. {
  11.     cout << title << ": ";
  12.     for (auto i : v)
  13.     {
  14.         cout << i << ' ';
  15.     }
  16. }
  17.  
  18. bool Gt2(int x)
  19. {
  20.     if (x > 2)
  21.     {
  22.         return true;
  23.     }
  24.  
  25.     return false;
  26. }
  27.  
  28. bool Lt2(int x)
  29. {
  30.     if (x < 2)
  31.     {
  32.         return true;
  33.     }
  34.  
  35.     return false;
  36. }
  37.  
  38. int main()
  39. {
  40.     vector<int> v = { 1,3,5,2,4 };
  41.    
  42.     cout << count(begin(v), end(v), 2);
  43.     cout << count_if(begin(v), end(v), Gt2);
  44.     cout << endl;
  45.     cout << count_if(begin(v), end(v), Lt2);
  46.    
  47.     int thr = 0;
  48.     cin >> thr;
  49.     cout << count_if(begin(v), end(v), [thr](int x)
  50.         {
  51.             if (x > thr)
  52.             {
  53.                 return true;
  54.             }
  55.  
  56.             return false;
  57.         });
  58.  
  59.     return 0;
  60. }
Tags: count count_if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement