STL Map
CoderSource.net
STL Map - Article by ub40
Level: BeginnerType: Article
Rating: Page: 1 of 1

Date: 7/21/2006 12:00:00 AM

Environment: Unix, Windows

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:

1

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us