Question
The program uses a map to store and accumulate the keys and values. In this example, we have 2 keys, iluvuic and AAA, and their
The program uses a map to store and accumulate the keys and values. In this example, we have 2 keys, "iluvuic" and "AAA", and their associated values are 80 and 225 respectively. Next the program allows the user to lookup by key, and if found, outputs the associated value in the map:
Enter key to lookup> 007 Not found... Enter key to lookup> iluvuic Found, value: 225 Enter key to lookup> AAA Found, value: 80 Enter key to lookup> aaa Not found... Enter key to lookup> #
Finally, the program outputs the contents of the map in ascending order by key (which is the default for maps):
Map: (AAA, 80) (iluvuic, 225)
// // using map to store, accumulate and lookup (key, value) pairs: //
#include
using namespace std;
int main() { string key; int value;
// // input (key, value) pairs and accumulate into map: // cout << "Enter key or #> "; cin >> key;
while (key != "#") { cout << "Enter value> "; cin >> value;
// // TODO: use map to store / accumulate (key, value) pair: //
cout << "Enter key or #> "; cin >> key; }
// // now allow user to search for key and retrieve value: // cout << endl; cout << "Enter key to lookup, or #> "; cin >> key;
while (key != "#") { // // TODO: use map to lookup key... //
cout << "Enter key to lookup, or #> "; cin >> key; } // // Finally, let's print the map to the console: // // // TODO: //
// // done: // return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started