fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <queue>
  5.  
  6. using namespace std;
  7.  
  8. using freqMap = map<char, int>;
  9. using freqMapElement = typename freqMap::value_type;
  10.  
  11. struct cmpByValue
  12. {
  13. bool operator()(const freqMapElement* p1, const freqMapElement* p2)
  14. {
  15. return p1->second > p2->second;
  16. }
  17. };
  18.  
  19. int main()
  20. {
  21. string s = "ASDASDASD";
  22.  
  23. freqMap freq_map;
  24. for (auto c : s)
  25. {
  26. freq_map[c]++;
  27. }
  28.  
  29. priority_queue<
  30. freqMapElement*,
  31. std::vector<freqMapElement*>,
  32. cmpByValue> pq;
  33.  
  34. for (auto &elem : freq_map)
  35. {
  36. pq.push(&elem);
  37. }
  38.  
  39. freqMapElement* top = pq.top();
  40. cout << top->first << ' ' << top->second << endl;
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
A 3