Skip to content
Justice Tech School

Practice: Top Scores

Sort a vector of pairs and print the top two.

Example
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>

int main() {
  std::vector<std::pair<int, std::string>> v = {{70, "Tunde"}, {92, "Ada"}, {85, "Amina"}};
  std::sort(v.rbegin(), v.rend());
  for (int i = 0; i < 2; i++) std::cout << v[i].second << " " << v[i].first << "\n";
  return 0;
}

Result

Ada 92
Amina 85

Exercise

Sort descending.

sort(v.(), v.rend())

Quiz Test

How does sort on reverse iterators order?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.