Page tags

Add a new page

creating a dictionary type with auto-find function in the form of (key, value) pair

automatically sort data

The following program counts the frequency of each word.

#include <iostream>#include <map>#include <string>intmain(){std::stringword;
std::map<std::string, int> count; // Count the frequency of each wordwhile(std::cin >> word){count[word]++;
// If 'word' appears for the first time, count[word] will create an entry (word, 0)// since default constructor for int initialises value to 0.// Then the value of count[word] is incremented by 1, results in (word, 1)// Otherwise, it will increment the value of count.find(word)->second by 1.}// This will print all words and their corresponding frequency line by line.// Results will be printed in alphabetical order of words.for(std::map<std::string, int>::const_iteratoritr = count.begin();
itr != count.end(); ++itr){std::cout << itr->first << "\t" << itr->second << std::endl;
}return0;
}