Edit: number of elements to be displayed can be user defined and default to 10 but can be defined to be a very big number.
I have a file which I parse for words, then I need to count how many times each word appeared in the text and display the 10 words with the highs number of appearances(using C++).
I currently insert each parsed word into a std::map, the word is the key and the number of its appearances is the value. Each time I come across a word that is not part of the std::map I add it with the initial value of 1 and each time I come across a word that is part of the map I add 1 to its current value.
After I am done parsing the file I have a map with all the unique words in the text and the number of their appearances but the map is not sorted by the value of the keys.
At this point I can traverse the std::map and push its words into priority queue(ordered with min value at the top), Once the priority queue reaches maximum capacity of 10 words I check if the value I am about to insert is bigger then the value at the top and if so I pop the top and Insert the value(if not I move on to the next value from the std::map.
Because each word appears only once(at this stage) I know for sure that each value at the priority queue is unique.
My question is can this be done more efficiently in regards to complicity?