Lab 1 Q5 - Using STL : Maps

Maps

Maps are associative data structures which hold pairs of objects - a pair is a combination of a key and value where the key points to the value

Example 1 : Adding pairs and iterating over a map

For example we need to store all the course codes and their names as in the following table

Course Code Course Name
SFDV2001 Web Development
SFDV2004 Application software Development
SFDV4001 OOP with C++ and UI

In the above table we can make course code the key and course name the value and we use the key to access the value.

To add the following data to our C++ programs we use the map class provided by STL as shown in the code below

map<string, string> courses;
//add a course and its name to the course map
courses.insert(pair<string,string> ("SFDV2001", "Web Development"));
 
//another way to add a course to the map
courses["SFDV2004"] = "Application Software Development";
courses["SFDV4001"] = "Object Oriented Programming with C++ and UI";
 
//iterate over the map pairs and print all the keys and values
map<string,string>::const_iterator coursesIterator = courses.begin();
while(coursesIterator != courses.end() ) {
    cout << "Course code:" << coursesIterator->first;
    cout << " Course Name:" << coursesIterator->second;
    cout << endl;
    coursesIterator++; //make the iterator point to the next pair in the map
}

You must increment the iterator (see the code above) so that the its points to the next pair in the map - if you do not it goes into an infinite loop. To test this remove the last line in the above code coursesIterator++; and see what happens.

Example 2 : Adding your own objects to the map

In the above example we have used string objects and added them to map.
In this example we see how to use objects from your classes and add them to them to map

We use the example of LineItems in a POS(Point of Sale) system - this is the kind of system you see at checkout in shop and supermarkets.

First we model the LineItems class as follows:

/* LineItem.cpp */
#include <iostream>
using  namespace std;
 
class LineItem {
    //represents an item in the supermarket
    private:
        int itemCode;
        string itemName;
        float itemPrice;
    public:
        LineItem(int,string,float);
        void printLineItem(); //print the value of all fields
};
 
LineItem :: LineItem(int code, string name, float price) {
    itemCode = code;
    itemName = name;
    itemPrice = price;
}
 
void LineItem::printLineItem() {
    cout << itemCode << ":" << itemName << ":" << itemPrice << endl;
}
 
//main() to test the class
/*
int main() {
    LineItem item(101,"Shine Washing Powder", 0.750);
    item.printLineItem();
}
*/

Now we write a program to illustrate how we can use the above LineItem class in the map.
Assume that for a given item code the program should print the name and price of the item - see the program below

#include <iostream>
#include <map>
#include "LineItem.cpp"
 
/*
 * This example shows you how to put your own objects into a map
 * using some unique key
 */
int main(){
   map<int, LineItem> items;
 
   //create some LineItem objects that can be added to the map
   LineItem washingPowder(101, "Shine Washing Powder", 0.750);
   LineItem toothpaste(202, "Toothpaste", 0.500);
   LineItem eggs(303, "Eggs 6", 0.500);
 
   //add some LineItem objects to the items map - key is the item id a
   //the value is the object itself
   items.insert(pair<int, LineItem> (101, washingPowder));
   items.insert(pair<int, LineItem> (202, toothpaste));
   items.insert(pair<int, LineItem> (303, eggs));
 
   cout << "Size of the items map : " << items.size() << endl;
 
   //iterate over the map pairs and print all the keys and values
    map<int,LineItem>::const_iterator itemsIterator = items.begin();
    while(itemsIterator != items.end() ) {
        LineItem item = itemsIterator->second;
        item.printLineItem();
        itemsIterator++; //make the iterator point to the next pair in the map
    }
}

Example 3: Finding a pair in a map

To find a pair in a map use the find() method of the map class. find() returns the iterator to the pair. From the pair you can get the value using the ->second on the iterator

See the example code below on how to use find to print the course name for SFDV2004 for the above Example 1

string courseCode = courses.find("SFDV2004") -> first;
string courseName = courses.find("SFDV2004") -> second;
cout << courseCode << " " << courseName << endl;

Another example of find() for the above Example 2

//finding an item using the key
LineItem li = items.find(101) -> second;
li.printLineItem();

Exercises

Exercise 1 - Department and phone extensions using maps

Write a C++ program to create a map and add the following data about some of the extensions on the college PBX (the local phone system). Then print all the data in the map using an iterator as shown in the above examples.

Department Extension
LRC 101
IT 107
Cafeteria 402
Registration 404

Exercise 2 - Acronyms and their full forms using maps

Write a C++ program to create a map and add the following pairs of data to it about acronyms used in IT and and their full form. Print all the data in the map using the iterator.

Acronym Full / Expanded Form
TCP Transmission Control Protocol
STL Standard Template Library
API Application Programming Interface
IDE Integrated Development Environment
TLA Three Letter Acronym

Exercise 3 - Rial to foreign currency converter

Write a C++ program that reads in the amount of Omani Rials to convert into a foreign currency such as dollars, pounds etc. and outputs the amount in the input foreign currency.
Also read in the foreign currency from the keyboard using the codes given in the table below
Conversion should be done according to the following table

Foreign Currency Conversion Rate
USD 2.5
AUD 3.0
GBP 1.5
EUR 1.3
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License