Advertisement
RobertDeMilo

YB4.1 Введение в итераторы

Nov 10th, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct Lang
  8. {
  9.     string name;
  10.     int age;
  11. };
  12.  
  13. int main()
  14. {
  15.     vector<string> langs = { "Python", "C++", "C", "Java", "C#" };
  16.  
  17.     auto result = find_if(begin(langs), end(langs), [](const string& lang)
  18.         {return lang[0] == 'C'; });
  19.  
  20.     /*cout << *result << endl;*/
  21.     string& ref = *result;
  22.     ref = "D++";
  23.     cout << *result << endl;
  24.     #######################################################################################################
  25.  
  26.     vector<Lang> langs = { {"Python", 26}, {"C++", 34}, {"C", 45}, {"Java", 22}, {"C#", 17} };
  27.     auto result = find_if(begin(langs), end(langs), [](const Lang& lang)
  28.             {return lang.name[0] == 'C'; });
  29.  
  30.     if (result == end(langs))
  31.     {
  32.         cout << "Not found" << endl;
  33.     }
  34.     else
  35.     {
  36.         cout << (*result).age << endl;
  37.         cout << result->age << endl;
  38.     }
  39.    
  40.     cout << begin(langs)->name << " " << begin(langs)->age << endl;
  41.     cout << langs.begin()->name << " " << langs.begin()->age << endl;
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement