teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

修正

2021/06/25 21:57

投稿

episteme
episteme

スコア16612

answer CHANGED
@@ -24,17 +24,11 @@
24
24
  }
25
25
  }
26
26
 
27
- // map<string,int> -> vector<pair<int,string>
28
- std::vector<std::pair<int,string>> hist(words.size());
27
+ std::vector<std::pair<string,int>> hist(words.begin(), words.end());
29
- std::transform(words.begin(), words.end(), hist.begin(),
30
- [](const auto& item) { return std::make_pair(item.second, item.first); });
31
- // partial sort
32
28
  std::partial_sort(hist.begin(), hist.begin()+10, hist.end(),
33
- [](const auto& x, const auto& y) { return x.first > y.first; });
29
+ [](const auto& x, const auto& y) { return x.second > y.second; });
34
- // print
35
30
  for_each(hist.begin(), hist.begin()+10,
36
- [](const auto& item) { std::cout << item.first << " : "
31
+ [](const auto& item) { std::cout << item.second << " : "
37
- << item.second << std::endl; });
32
+ << item.first << std::endl; });
38
- return 0;
39
33
  }
40
34
  ```