Advertisement
chevengur

CПРИНТ №1 | Базовые алгоритмы | Урок 4: Алгоритмы sort и reverse 1/2

Sep 5th, 2023 (edited)
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     int count;
  11.     cin >> count;
  12.     vector<pair<string, int>>candidate;
  13.    
  14.     for (int i = 0; i < count; ++i) {
  15.         string name;
  16.         int age;
  17.         cin >> name >> age;
  18.        
  19.         candidate.push_back({ name, age });
  20.     }
  21.  
  22.     sort(candidate.begin(), candidate.end(), [](const pair<string, int>& one, const pair<string, int>&two) {
  23.         return (one.second == two.second) ? one.first > two.first: one.second > two.second;         //тернарный оператор
  24.         });
  25.     for (const auto& [name, age] : candidate) {
  26.         std::cout << name << endl;
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement