So I need to find some most common used words in a file.
I have a vector<pair<string, int> > wordList
which keeps track of every word in the file along with its frequency. This part works fine.
The problem is, the output shows multiple versions of each word. This is because the way I calculated it was:
- load all words into the vector with a frequency of 1
- Go through again and if the word shows up twice, increase its count
The part that I need help on, which is to delete multiple entries of the same word.
for(int j = 0; j < wordList.size(); j++) {
This is my current approach. This function tallys up all the word. the problem is that the line wordList.erase
that's inside the for loop
produces an out of bounds error, so I cant remove the duplicate entry that way.
I also tried the unique() approach, but that doesn't seem to work it only removes some entries.
What is the most efficient way to reduce a vector of pairs to only unique elements?