Maps can store only unique keys.
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> mapObject;
pair<map<char,int>::iterator, bool> res;
res = mapObject.insert(pair<char, int>('A', 65));
if(res.second)
cout << "Insertion occured.\n";
res = mapObject.insert(pair<char, int>('A', 99));
if(!res.second)
cout << "Duplicate not allowed.\n";
map<char, int>::iterator p;
p = mapObject.find('A');
cout << "Its ASCII value is " << p->second;
return 0;
}
Related examples in the same category