title: How can I push_back a map<string, int> into a vector<map<string, int>> via an iterator?
body: <p>I'm currently studying the book <em>Accelerated C++</em> (Koening/Moo) and I'm having trouble with one of the exercises. The task is to write a program which takes as an input some sequence of words which it then stores in a <code>map&lt;string, int&gt;</code>. The strings are the words entered and the associated <code>int</code> is the number of times each word occurs. You then have to sort the words by the number of times they occur; that is, by the value and not the key. You can't sort a map by the value, so I tried to copy the elements to a vector instead, which I intended to sort using a predicate. Unfortunately, all I get is a screen full of errors from g++. They seem to stem from the same place - putting the elements of my map into my vector, which I try to do like this:</p>
  
  <pre><code>int main()
  {
      map&lt;string, int&gt; counters;
  
      cout &lt;&lt; "Enter some words followed by end-of-file (ctrl-d): ";
      string word;
      while (cin &gt;&gt; word)
         ++counters[word];
  
      // Maps cannot be sorted by values, so pass the elements of counters to a vector.
      vector&lt;map&lt;string, int&gt; &gt; vec_counters;
  
      map&lt;string, int&gt;::const_iterator it = counters.begin();
      while (it != counters.end()) {
         vec_counters.push_back(*it);
         ++it;
      }
  }
  </code></pre>
  
  <p>This is obviously just the first part, but I can't get even this to compile. I get the following error:</p>
  
  <blockquote>
    <p>32:31: error: no matching function for call to       std::vector, int> >::push_back(const std::pair, int>&amp;)’
    /usr/include/c++/4.5/bits/stl_vector.h:741:7: note: candidate is: void std::vector&lt;_Tp, _Alloc>::push_back(const value_type&amp;) [with _Tp = std::map, int>, _Alloc = std::allocator, int> >, value_type = std::map, int>]</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>