# Connexions

You are here: Home » Content » C++ STL map with multiple keys

### Recently Viewed

This feature requires Javascript to be enabled.

# C++ STL map with multiple keys

Module by: Brian West. E-mail the author

Summary: This article shows to get a STL map to work with multiple keys. Instead of having a single key mapped to a single value, a STL map will be shown that maps multiple keys to a single value. Two ways to accomplish will be shown.

The C++ STL map is an associative map between a single key and a single value. In addition, the key must be unique for the given map. The C++ STL also provides a multimap template, which removes the unique key restriction. A single key in a multimap can map to multiple values. The STL multimap name might confuse the gentle reader into believing that it supports a map with multiple keys, which it does not. A map with multiple keys can be realized with the STL map as shown below.

Most examples of the C++ STL map show a single key mapping to a single value. The map key is usually shown as a simple data type like an integer or a string, but the key also can be an aggregate object. In particular, the key object can be a struct/class containing multiple keys. For example,

  class MultiKey { public: long key1; int key2; long key3; long key4; MultiKey(long k1, int k2, long k3, long k4) : key1(k1), key2(k2), key3(k3), key4(k4) {} }; 

Naively, a multi-key map instance using the above definition can be constructed via

std::map<MultiKey, int> mymap;


The gentle reader will discover that the above C++ snippet will fail to compile. The missing magic is that a "less" comparator function/method (aka the less than operator) needs to be supplied.

Requiring a the less than operator is a bit counter intuitive. The STL map must be able to compare two keys for equality, to maintain key uniqueness when adding to the map. The equality operator can be defined via the less than operator, as will be shown below.

Internally the STL map is sorted by the key via the less than operator. Sorting makes determining membership efficient. Without key sorting, determining membership would require accessing all map entries, worst case. With key sorting and an appropriate data structure like a btree, the membership becomes more efficient.

There are multiple ways to add the less than operator to the above example. Adding a operator<() to the above class is the most straight forward.

  class MultiKey { public: long key1; int key2; long key3; long key4; MultiKey(long k1, int k2, long k3, long k4) : key1(k1), key2(k2), key3(k3), key4(k4) {} bool operator<(const MultiKey &right) const { if ( key1 == right.key1 ) { if ( key2 == right.key2 ) { if ( key3 == right.key3 ) { return key4 < right.key4; } else { return key3 < right.key3; } } else { return key2 < right.key2; } } else { return key1 < right.key1; } } }; std::map mymap; 

Note the implementation of operator<(). The individual keys are given a precedence while checking for less than. A straight forward but incorrect implementation of less than looks like this:

  bool operator<(const MultiKey &right) const { return ( key1 < right.key1; && key2 < right.key2; && key3 < right.key3; && key4 < right.key4; ); } 

The above code fails when comparing the results of multikey1 < multikey2 and multikey2 < multikey1. This failure also will cause problems when checking for key equality in the membership test. As mentioned above, the less than operator is used to make the equality calculation. The equality operator is implemented something like this:

  bool operator==(const MultiKey &right) const { return ( !(this < right) && !(right < this) ); } 

Thus, multi-keys are equal when neither is less than the other.

## An alternative less than operator

When declaring a map instance, the less than operator can be explicitly provided like this:

 class MultiKey { public: long key1; int key2; long key3; long key4; MultiKey(long k1, int k2, long k3, long k4) : key1(k1), key2(k2), key3(k3), key4(k4) {} bool operator<(const MultiKey &right) const { if ( key1 == right.key1 ) { if ( key2 == right.key2 ) { if ( key3 == right.key3 ) { return key4 < right.key4; } else { return key3 < right.key3; } } else { return key2 < right.key2; } } else { return key1 < right.key1; } } }; struct multikey_less : public std::binary_function { bool operator()(const MultiKey &mkey1, const MultiKey &mkey2 ) const { return mkey1.operator<(mkey2); // mkey1 < mkey2; } }; std::map mymap2; 

I did not want to re-implement the class MultiKey so multikey_less just calls MultiKey's less than operator. Obviously, if the less than operator existed and it did what you needed, there would be no need for creating multikey_less. If the existing less than operator is not what is needed though, it can be overridden via a less than comparator like multikey_less above (but with an independent implementation), supplied on the instance declaration.

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks