Advertisement
chevengur

Вводный курс: основы C++ | Урок 4: Словари и константность

Sep 4th, 2023 (edited)
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. string FindWidespreadBird(const vector<string>& types) {
  10.     map<string, int>birds;
  11.     for (const auto& type : types) {
  12.         ++birds[type];
  13.     }
  14.     auto pr = max_element(birds.begin(), birds.end(), [](const auto&x, const auto& y) {           //алгоритмом max_element и лямбдой                                                                                       
  15.                                                                                                   //нахожу ключ, значение которого
  16.                                                                                                   //больше всех
  17.         return x.second < y.second;
  18.         });
  19.    
  20.     string bird = pr->first;
  21.     return bird;
  22. }
  23.  
  24. int main() {
  25.     vector<string> bird_types1 = { "zyablik"s, "sinica"s, "vorobey"s, "zyablik"s, "sinica"s, "sinica"s };
  26.     if (FindWidespreadBird(bird_types1) == "sinica"s) {
  27.         cout << "Correct"s << endl;
  28.     }
  29.     else {
  30.         cout << "Not correct"s << endl;
  31.     }
  32.  
  33.     vector<string> bird_types2 = { "ruh"s, "sirin"s, "blue bird of fortune"s, "finist"s, "fenix"s };
  34.     if (FindWidespreadBird(bird_types2) == "blue bird of fortune"s) {
  35.         cout << "Correct"s << endl;
  36.     }
  37.     else {
  38.         cout << "Not correct"s << endl;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement