Loop through map and print all the key/value pair : map iterator « map multimap « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

#include <map> // map class-template definition

int main()
{
   std::map< int, double, std::less< int > > pairs;

   pairs.insert( std::map< int, double, std::less< int > >::value_type( 15, 2.7 ) );
   pairs.insert( std::map< int, double, std::less< int > >::value_type( 30, 111.11 ) );
   pairs.insert( std::map< int, double, std::less< int > >::value_type( 0, 1010.1 ) );
   pairs.insert( std::map< int, double, std::less< int > >::value_type( 10, 22.22 ) );
   pairs.insert( std::map< int, double, std::less< int > >::value_type( 25, 33.333 ) );
   pairs.insert( std::map< int, double, std::less< int > >::value_type( 0, 77.54 ) ); // dup ignored
   pairs.insert( std::map< int, double, std::less< int > >::value_type( 20, 9.345 ) );
   pairs.insert( std::map< int, double, std::less< int > >::value_type( 15, 99.3 ) ); // dup ignored

   cout << "pairs contains:\nKey\tValue\n";

   // use const_iterator to walk through elements of pairs
   for ( std::map< int, double, std::less< int > >::const_iterator iter = pairs.begin();
      iter != pairs.end(); ++iter )
      cout << iter->first << '\t' << iter->second << '\n';

   cout << endl;
   return 0;
}
pairs contains:
Key     Value
0       1010.1
10      22.22
15      2.7
20      9.345
25      33.333
30      111.11








23.7.map iterator
23.7.1.Use iterator to loop through map and print all elements
23.7.2.Loop through map and print all the key/value pair
23.7.3.Display the first element in map
23.7.4.Display the last element in map
23.7.5.reverse_iterator from map
23.7.6.Cycle through the map in the reverse direction
23.7.7.Declare a reverse iterator to a map
23.7.8.Declare an iterator to a map
23.7.9.const_iterator for map
23.7.10.Create int string map and print all element pair