STL Map by ub40


STL Map

A map is a sorted unique associative container that maintains a collection of key value pairs.

These collections are sorted by the key. These collections are unique as only one value is allowed in the collection for the key.

 

Fundamentally, the most frequently used member of the STL’s map API is the [] operator. This operator allows convenient access and modification of a key’s associated value. If no value for the key specified exists, the key is associated with a default constructor and returns a reference to the new value. If a value is associated to the key specified, a reference to that value is returned. Maps are, therefore, useful for implementing collections of one-to-one mappings.

In the following STL example using STL Map, the Person class has a default c++ constructor that initializes an object of this class to a default null value, which has a blank name, a negative age value and a blank National Insurance Number. The program allows the user to enter some values of Person class into a map that associated the value of the name member to the objects . Then the program allows the user to enter some key values and returns the references to the associated objects.

#include <map>
#include <iostream>
#include <string>

using namespace std;

class Person
{
private:
string name;
int age;
string nINumber;

public:
Person(void)
{
name = "";
age = -1;
nINumber = "";
}

Person(string inName, int inAge, string inNINumber)
{
name = inName;
age = inAge;
nINumber = inNINumber;
}

string& getName(void)
{
return name;
}

int getAge(void)
{
return age;
}

string& getNINumber(void)
{
return nINumber;
}

bool operator == (const Person& p)
{
return (name == p.name);
}

bool operator < (const Person& p)
{
return (age < p.age);
}

bool isNULL(void)
{
return ((name == "") && (age == -1) && (nINumber == ""));
}
};

void populatePeople(map<string, Person>& peopleMap)
{
char continueFlag = 'y';
string name;
int age;
string nINumber;
while (continueFlag == 'y')
{
cout << "Enter name ";
cin >> name;
cout << "Enter age ";
cin >> age;
cout << "Enter National Insurance number ";
cin >> nINumber;

Person p(name, age, nINumber);
peopleMap[name] = p;

cout << "Enter y to add another, any other key to exit:";
cin.get();
continueFlag = cin.get();
cin.get();
}
}

void interrogateMap(map<string, Person>& peopleMap)
{
char continueFlag = 'y';
string name;
while (continueFlag == 'y')
{
cout << "Enter name to search ";
cin >> name;
Person p = peopleMap[name];
if (p.isNULL())
{
cout << "No entry found for " << name << endl;
}
else
{
cout << p.getName() << ":" << p.getAge() << ":" << p.getNINumber() << endl;
}

cout << "Enter y to find another, any other key to exit:";
cin.get();
continueFlag = cin.get();
cin.get();
}
}

int main(void)
{
map<string, Person> peopleMap;
populatePeople(peopleMap);
interrogateMap(peopleMap);
return(0);
}

An interaction with this program is listed below,

Enter name Omar
Enter age 38
Enter National Insurance number 3157
Enter y to add another, any other key to exit:y
Enter name Amna
Enter age 33
Enter National Insurance number 7531
Enter y to add another, any other key to exit:y
Enter name Inde
Enter age 30
Enter National Insurance number 1982
Enter y to add another, any other key to exit:n
Enter name to search Inde
Inde:30:1982
Enter y to find another, any other key to exit:y
Enter name to search Sajid
No entry found for Sajid
Enter y to find another, any other key to exit:y
Enter name to search Amna
Amna:33:7531
Enter y to find another, any other key to exit: