University of Leeds

Maps

Introduction

With the containers we seen so far (strings, arrays and vectors) we can select a specific element in the container by passing in an index e.g. a[6]. In a lot of common use cases, this is fine. However, consider an array containing student grades. How do we remember which index corresponds to which student? In this case, it would be better to use a map. A map is similar to an array, but instead the elements in the map are accessed using a key. This is often a string (e.g. the student surname). It should be noted that keys must be unique. We often talk about key-value pairs. In this case, the value would be student grade. The C++ standard library contains the map class.

It can be used by including the following header.

#include <map>

Creating a map

The first step is to specify the map structure. In many cases, the key will be a string and the value will be the appropriate datatype for the data being stored in the map.

// create a map of integers
  std::map<std::string, int> int_map;
  // add some value/key pairs
  int_map["Evans"] = 39;
  int_map["Smith"] = 65;
  int_map["Peters"] = 80;

Inserting and erasing

We can easily insert new key/value pairs using the insert method and erase pairs by supplying the key to the pair to be erased.

  // can also add elements using insert()
  int_map.insert(std::pair<std::string, int>("Davies", 45));
  // and erase specific elements by supplying a key
  int_map.erase("Evans");

Iterating

As with other containers, we can use an iterator from the C++ STL to iterate over elements in a map. We use the ->first and ->second variables to access the key and value respectively.

  // create an iterator to go through the map
  std::map<std::string, int>::iterator i;
  // iterate through and print
  for (i = int_map.begin(); i != int_map.end(); i++) {
    std::cout << "Key = '" << i->first << "' Value = " << i->second
              << std::endl;
  }

This would print the following output.

Key = 'Davies' Value = 45
Key = 'Peters' Value = 80
Key = 'Smith' Value = 65

Accessing values

We can extract values simply by providing the key to the value we wish to access.

// can access a specific value by providing a key
  int score = int_map["Peters"];
  std::cout << "Score = " << score << std::endl;

Checking if keys exist

Before trying to access a value, it is good practice to check whether the key actually exists. We can do this using the count() method.

// we can check whether a certain key exists
  std::string key = "Clarke";
  // the count() methods returns 0 if none are found
  if (int_map.count(key) == 0) {
    std::cout << key << " does not exist in the map\n";
  } else {
    std::cout << key << " does exist and has a value of " << int_map[key]
              << std::endl;
  }

In this case, the output would be

Clarke does not exist in the map

If we changed the key to one that we knew existed in the map

// try another key
  key = "Smith";

We would obtain the following output.

Smith does exist in the map and has a value of 65