How do I create a frequency table of strings?

I need to write a piece of code counts the string inputted and shows how many times each string occurred, essentially a frequency table of strings. So, user enters multiple strings (i.e. aaa, bbb, ccc, aaa) and the code is supposed to print out how many times each string occurs.

I know that I am supposed to use maps, but I have no clue how to proceed. I've looked at examples of frequency tables using maps, but I am lost. Any help would be greatly appreciated. Thank you!
Last edited on
You did gove example that has four strings. From those you would create a table that has two column; the string and its frequency:
1
2
3
aaa 2
bbb 1
ccc 1

A std::map has a list of pairs. Each pair has a key and a value. Keys are unique, just like the strings in the above table.

See
http://www.cplusplus.com/reference/map/map/operator[]/
http://en.cppreference.com/w/cpp/container/map/operator_at
I understand that keys are unique. But, am I able to generate keys automatically for each new entry that the user enters? I still don't know how I am supposed to use map to make a frequency table..
Last edited on
What does the (linked) descriptions of map::operator[] say about the case when you call it with value that is not an existing key?
Okay, so I went scouring through my notes and I found code that translated to a frequency table of strings. But, I'm having a bit of trouble.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

int main() {
map<string, int> F;  //Stores frequency table
string s;                  //holds input;

while (cin>> s) F[s]++;

for (map<string, int>::iterator i=F.begin(); i != F.end(); i++){
      cout<< (*i).first << ":\t" << (*i).second<< "\n";
}

return 0;

}


I tried the code, it works, but I don't understand it.

I understand that .first prints the key, and .second gives the value. But, I don't understand the purpose of the while loop.

As for the for loop: What is the purpose of the iterator? What's the point of printing out *i?
The while loop puts strings s into the map. If s isn't yet in the map it creates F[s] and sets its value to 1; if it is already there then it increases the value by 1. So it creates a frequency table. The while loop will run until cin can supply no more strings; then the condition of the stream will become 'false'.

The for loop outputs the frequency table by listing the pairs in the map. An iterator allows you to go through a container one by one when you don't have a natural array index. An iterator is a bit like a pointer, so you would get the element it points to by dereferencing as *i. Each element is a pair, with the first part accessed with '.first' and the second with '.second'. The iterator notation is incredibly verbose. You would get the same from
for ( auto e : F ) cout << e.first << ":\t" << e.second << '\n'
which says: for each element in F print out the key (here, a string), then a colon and tab, then the value (here, the count).

Different case - upper or lower - will give different strings, so depending on what you want to do you may want to filter the strings before putting them in the map.
Last edited on
Topic archived. No new replies allowed.