fork(3) download
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. template <typename M, typename V>
  8. void mapValToVec(const M &m, V &v){
  9. for (typename M::const_iterator it = m.begin(); it != m.end(); it++)
  10. {
  11. v.push_back(*it);
  12. }
  13. }
  14. bool comp(const pair<int, int> &a, const pair<int, int> &b){
  15. return a.second < b.second;
  16. }
  17. int main() {
  18. typedef map<int, int> Map;
  19. Map m = {{21, 55}, {11, 44}, {33, 11}, {10, 5}};
  20. vector<pair<int, int>> v;
  21. mapValToVec(m, v);
  22. std::partial_sort(v.begin(), v.begin()+3, v.end(), comp);
  23. for(int i = 0; i < 3; i++) cout << v[i].first << " " << v[i].second << " " <<endl;
  24. return 0;
  25. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
10 5 
33 11 
11 44