#include #include // for the sqrt() function, which returns a double #include using namespace std; int main() { // the map is also called a "hash" in other languages // (such as Perl) map< int, double > sqrts; map< int, double >::iterator i; while ( true ) { cout << "Enter an int and I'll tell you the sqrt: "; int n; cin >> n; i = sqrts.find( n ); // is n already a key in the map? // sqrts.end() is a "placeholder" BEYOND the // last element in the map (or list, vector, etc.) if ( i == sqrts.end() ) // not found { cout << "Oooo! That's new. I'll add it to the map.\n"; sqrts[n] = sqrt( (double)n ); i = sqrts.find( n ); } else { cout << "I already know that one.\n"; } cout << "sqrt of " << n << " is " << i->second << '\n'; } return 0; }