Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <iterator>
- #include <vector>
- #include <iomanip>
- int main()
- {
- std::vector<std::string> Words{"the", "page", "title", "page", "the", "page", "body"};
- std::cout << "\n" << "Vem är bäst?" << "\n";
- std::map<std::string,int> freq;
- for (auto c : Words)
- {
- // check if key 'c' exists in the map or not
- std::map<std::string,int>::iterator it = freq.find(c);
- // key already present in the map
- if (it != freq.end()) {
- it->second++; // increment map's value for key 'c'
- }
- // key not found
- else {
- freq.insert(std::pair<std::string,int>(c,1));
- }
- }
- for (auto e: freq) {
- std::cout.width(6);
- std::cout << std::fixed << std::left << e.first << " " << std::right << e.second << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement