#include #include using namespace std; struct node { int i; double d; node() : i( 0 ), d( 0 ) {} node( int i, double d ) : i( i ), d( d ) {} }; typedef map< char, node > mapchar; ostream &operator<<( ostream &os, const node &n ) { return os << "i:" << n.i << " d:" << n.d; } void printMap( const mapchar &info ) { if ( info.empty() ) { cout << "Map is empty" << endl; } else { mapchar::const_iterator iter; for ( iter = info.begin(); iter != info.end(); ++iter ) { cout << iter->first << '\t' << iter->second << endl; } // for } // if } // printMap int main() { mapchar top; // create map int i; for ( i = 0; i < 10; i += 1 ) { // create map nodes node n( i, (double)i + 0.5 ); top[ 'a' + i ] = n; // copy node into map } // for printMap( top ); cout << endl; if ( top.count( 'd' ) != 0 ) { // 0 or 1 for unique key cout << top[ 'd' ] << " is associated with key 'd'" << endl; } if ( top.count( 'z' ) == 0 ) { // 0 or 1 for unique key cout << "no value associated with key 'z'" << endl; } // if mapchar::iterator where = top.find( 'c' ); // find location of key if ( where != top.end() ) { // found ? cout << "found " << where->first << ' ' << where->second << endl; } // if cout << "erase map" << endl; top.clear(); printMap( top ); } // main