Cay Horstmann & Timothy Budd

Laboratory Notebook
Chapter 23 – The Standard Template Library - Containers

Your name:

Your email address:

Your student ID number:

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.

Lab Objectives

To gain experience with


        understanding the purpose and use of the containers in the Standard Template Library

        the fundamental sequential containers—vector, list, and deque

        the adapter containers—stack, queue, and priority queue

        the ordered container—set

        the associative container—map


P1. Introduction to Maps


Maps can be very useful in the proper situation. To illustrate, write a class to represent a list of secret agents. The class should use a map to store operative names and their ID numbers. Store each pair (an integer and a string) within a map such that the ID number serves as the key. The class definition is given below.


class AgentList




string getAgentName(int ID);

void addAgent(int ID, string name);


map<int, string> agentList;




Submit all of the code for your answer in the space below.


R1. Fundamental Containers


Name the three fundamental containers. Then, re-implement your agent list class using each of these fundamental containers in place of the map. You will find that it is important to choose the right container for the right job as doing so can significantly reduce the complexity of your programming.

Name the three fundamental containers.

Submit code for the AgentList class using container one in the space below.

Submit code for the AgentList class using container two in the space below.

Submit code for the AgentList class using container three in the space below.


P2. Map Interaction


Returning to the implementation in Problem P1, modify your AgentList class to include a map for storing agent names and their native country (both will be string values). The name will be used as a key to determine the country of origin. The class definition is provided below. For the getAgentCountry() function, you should use the ID to retrieve the name from one map. That name should then be used to retrieve the country from the other map.


class AgentList




string getAgentName(int ID);

string getAgentCountry(int ID);

void addAgent(int ID, string name, string country);


map<int, string> agentNameList;

map<string, string> agentCountryList;


Provide all code in the space below.


R2. Adapters


In addition to the fundamental containers, there are also a number of structures for representing grouped items. These additional structures are called adapters. In this problem, supply implementations for your AgentList class developed in Problem P2 using a stack and queue in place of a map.

Provide your AgentList class using a stack in the space below.

Provide your AgentList class using a queue in the space below.


P3. Testing


Write a short program to test your AgentList classes. Ensure that your tests guarantee the complete functionality of each implementation.


Insert all code in the space below.


R3. Iterators


Do you remember how to use iterators? The Map class used within your AgentList class supplies such a feature. Modify your AgentList class to include a new member function called print(). This function should use an iterator to traverse the private map and display information contained within the AgentList class to the screen.


Provide your answer in the space below.


P4. Practice with Sets

Suppose that you run a secret government organization whose purpose is to catalog the secret agents of other countries. Every week, employees of the agency submit a list of agents that they have uncovered. Write a program that collects these submitted agent lists and inserts each element into a set container. Traverse the set checking for duplicate agents with the same ID but different name in the set. Note that duplicate agents with same ID and name will not appear in the set because sets do not allow insertion of duplicate keys. If a duplicate agent (same ID, different name) appears in the set, remove that agent from the set. A duplicate agent with different information will be considered unreliable data and must not be included in the master set.

Submit all code in the space below.


Do not forget to send your answers when you are finished.