Using subscript to add value to map - C++ STL

C++ examples for STL:map

Description

Using subscript to add value to map

Demo Code

#include <iostream> 
#include <map> // map class-template definition 
using namespace std; 

// define short name for map type used in this program 
typedef map< int, double, less< int > > Mid; 

int main() //  w  w w.j  av  a  2s . c  om
{ 
   Mid pairs; 
   pairs.insert( Mid::value_type( 15, 2.7 ) ); 
   pairs.insert( Mid::value_type( 30, 111.11 ) ); 

   pairs[ 25 ] = 9999.99; // use subscripting to change value for key 25 
   pairs[ 40 ] = 8765.43; // use subscripting to insert value for key 40 

   cout << "\nAfter subscript operations, pairs contains:\nKey\tValue\n"; 

   // use const_iterator to walk through elements of pairs 
   for ( Mid::const_iterator iter2 = pairs.begin();iter2 != pairs.end(); ++iter2 ) 
       cout << iter2->first << '\t' << iter2->second << '\n'; 

   cout << endl; 
}

Result


Related Tutorials