Advertisement
Guest User

Untitled

a guest
May 24th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. /*
  2. * topn_lite.cpp
  3. * Copyright (C) 2015 Emiliano Firmino <emiliano.firmino@gmail.com>
  4. *
  5. * Distributed under terms of the MIT license.
  6. */
  7.  
  8. #include <algorithm>
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <unordered_map>
  13. #include <vector>
  14.  
  15. int main(int argc, char ** argv) {
  16. std::fstream file;
  17. file.open(argv[1], std::fstream::in);
  18.  
  19. std::unordered_map<std::string, unsigned int> dict;
  20.  
  21. std::string word;
  22. while (file >> word) {
  23. std::transform(word.begin(), word.end(), word.begin(), ::tolower);
  24.  
  25. auto value = dict.find(word);
  26. if (value == dict.end() )
  27. dict.insert(std::make_pair(word, 1));
  28. else
  29. dict[word] = value->second + 1;
  30. }
  31.  
  32.  
  33. std::vector<std::pair<std::string, unsigned int>> list(dict.begin(), dict.end());
  34.  
  35. std::sort(list.begin(), list.end(),
  36. [](const std::pair<std::string, unsigned int>& lhs,
  37. const std::pair<std::string, unsigned int>& rhs) {
  38. return lhs.second > rhs.second;
  39. });
  40.  
  41. for (const auto &w : list) {
  42. std::cout << w.first << " : " << w.second << "\n";
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement