I am currently studying an accelerated C ++ book (Koenig / Moo) and I am having problems with one of the exercises. The problem is to write a program that takes as input some sequence of words, which it then stores on the map. Lines are words entered, and the number associated with it is the number of times each word occurs. Then you have to sort the words by the number of times; that is, by value, not by key. You cannot sort the map by value, so I tried to copy the elements into a vector instead, which I was going to sort using a predicate. Unfortunately, all I get is a screen full of errors from g ++. It seems that they come from the same place - putting the elements of my map into my vector, which I am trying to do as follows:
int main() { map<string, int> counters; cout << "Enter some words followed by end-of-file (ctrl-d): "; string word; while (cin >> word) ++counters[word];
This is obviously only the first part, but I canβt even compile it. I get an error message:
32:31: error: there is no corresponding function to call std :: vector, int β :: push_back (const std :: pair, int> &) / usr / include / c ++ / 4.5 / bits / stl_vector.h: 741 : 7: note: candidate: void std :: vector <_Tp, _Alloc> :: push_back (const value_type &) [with _Tp = std :: map, int>, _Alloc = std :: allocator, int β, value_type = std :: map, int>]
I think I'm doing something fundamentally stupid ... but I canβt see me for life that ..
Any help would be great!
C
source share