Advertisement
perjespersson

Map

Apr 3rd, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <iterator>
  4. #include <vector>
  5. #include <iomanip>
  6.  
  7. int main()
  8. {
  9.  
  10.   std::vector<std::string> Words{"the", "page", "title", "page", "the", "page", "body"};
  11.   std::cout << "\n" << "Vem Àr bÀst?" << "\n";
  12.  
  13.  
  14.   std::map<std::string,int> freq;
  15.   for (auto c : Words)
  16.     {
  17.         // check if key 'c' exists in the map or not
  18.         std::map<std::string,int>::iterator it = freq.find(c);
  19.  
  20.         // key already present in the map
  21.         if (it != freq.end()) {
  22.             it->second++;   // increment map's value for key 'c'
  23.         }
  24.         // key not found
  25.         else {
  26.       freq.insert(std::pair<std::string,int>(c,1));
  27.         }
  28.     }
  29.  
  30.     for (auto e: freq) {
  31.     std::cout.width(6);
  32.         std::cout << std::fixed << std::left << e.first << " " << std::right << e.second << '\n';
  33.     }
  34.  
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement