#include #include using namespace std; /* * maps are used to store 1:1 relationships * they are really sets with a pair * they are not random access, but the [] * means key indexing */ // overloading the output stream operator ostream & operator << ( ostream & ost, const map & l ) { map::const_iterator i = l.begin(); for ( ; i != l.end() ; ++i ) ost << i->first << ":" << i->second << endl; return ost; } int main() { map< int, float > m; for ( int i = 0 ; i < 10 ; ++i ) m.insert( make_pair( i, i * 1.01 ) ); cout << m << endl; // add some more for ( int i = 20 ; i < 30 ; ++i ) m[i] = i * 1.01; cout << m << endl; // show what value matches key 7 cout << m[7] << endl; // this overwrites the existing value m[7] = 17.888; // this adds a new key/value pair m[19] = 86.86; cout << m << endl; return 0; }