-1

how do I print a vector of multimaps? for example I have a vector that looks like this:

typedef std::multimap<double,std::string> resultMap;
typedef std::vector<resultMap> vector_results;

EDIT

for(vector_results::iterator i = vector_maps.begin(); i!= vector_maps.end(); i++)
{
   for(vector_results::iterator j = i->first.begin(); j!= i->second.end(); j++)
   {
      std::cout << j->first << " " << j->second <<std::endl;
   }
}
Praetorian
  • 106,671
  • 19
  • 240
  • 328
Abhi Shah
  • 29
  • 5
  • 1
    At least give it a go so we have something to work on where you cannot fix your particular problem. – Ed Heal Mar 07 '13 at 00:26
  • @EdHeal: I actually did. here is the try `for(vector_results::iterator i = vector_maps.begin(); i!= vector_maps.end(); i++){ for(vector_results::iterator j = i->first.begin(); j!= i->second.end(); j++){ std::cout << j->first << " " << j->second < – Abhi Shah Mar 07 '13 at 00:27
  • 1
    possible duplicate of [Pretty-print C++ STL containers](http://stackoverflow.com/questions/4850473/pretty-print-c-stl-containers) – ildjarn Mar 07 '13 at 00:27
  • 1
    @AbhiShah - Why not put that into the question. – Ed Heal Mar 07 '13 at 00:29
  • @AbhiShah `i` points to a `resultMap`, change the inner for loop to `for( resultMap::iterator j = i->begin(); j != i->end(); ++j )` – Praetorian Mar 07 '13 at 00:37
  • @Praetorian: thanks I tried that and it worked. Thanks – Abhi Shah Mar 07 '13 at 01:26

1 Answers1

1

The i variable in the outer for loop points to a resultMap, which means the type of the j variable needs to be resultMap::iterator. Rewrite your loops as

for(vector_results::const_iterator i = vector_maps.begin(); i != vector_maps.end(); ++i)
{
   for(resultMap::const_iterator j = i->begin(); j != i->end(); ++j)
   {
      std::cout << j->first << " " << j->second << std::endl;
   }
}

I changed the iterator type to const_iterator since the iterators are not modifying the container elements.


If your compiler supports C++11's range based for loops, the code can be written much more succinctly

for( auto const& i : vector_maps ) {
  for( auto const& j : i ) {
    std::cout << j.first << " " << j.second << std::endl;
  }
}
Praetorian
  • 106,671
  • 19
  • 240
  • 328