I would recommend you browse the documentation for std::map
found here. Then take a look at the examples provided in the insert()
method documentation here.
The declare a map
object, you need to specify the type for the key and the type for the value by providing the type name as template parameters:
#include <map>
using namespace std;
map<int,int> myMap;
If you then want to insert a key/value pair:
int myKey = 10;
int myVal = 100;
myMap.insert(pair<int,int>(myKey, myVal));
The above can be made a bit more terse with some typedefs:
typedef map<int,int> IntMap;
typedef pair<int,int> IntPair;
IntMap myMap;
myMap.insert(IntPair(10, 100));
If you want the key/value pairs to be provided by user input, just write a simple loop that accepts the values from standard input and insert the values into your map.
There are plenty of resources here for reading values from standard input. Something like the below would do the trick:
// pseudo-code
while (did the user quit?)
{
int key = 0;
int value = 0;
cin >> key >> value;
// maybe if the user enters -1, then you quit, otherwise:
myMap.insert(pair<int,int>(key, value));
}