C++ offers us a wide range and various categories of Containers, each with their own special set of uses. One such Category of C++ Containers are the Associative Containers. The reason they are called “Associative” Containers should be clear by the time you are done with this tutorial.

Associative containers in C++ is one that stores “Sorted Data”, in contrast to other Container types. Because of this, it is much faster to search through it and access the data. It does however mean, inserting data will take longer (to place it in the correct position).

All container types in C++ are class templates, which means they are very flexible with what kind of data type that can store. Furthermore, this means they all have the same declaration method, making it easy to learn, remember and work with several container types.


Set Container

What makes the C++ Set Container different from other Associative Containers is that it stores “unique and sorted” data. By unique, we mean there are no duplicates of the same data element in this container. Any duplicates which are entered will be automatically removed.

Below is a short piece of code which shows us the Set Container in action.

#include <iostream>
#include <set>
using namespace std;

int main() {
    set<int> s1 = {2, 1, 3, 4, 0, 1};
    cout << "OUTPUT: " << s1.size() << endl;

    if (!s1.empty()) {
        for (int i: s1) {
            cout << i << endl;
        }
    }
}
OUTPUT: 5
0
1
2
3
4

You can insert data into the Set Container by using the insert() function.

    set<int> s1 = {2, 1, 3, 4, 0, 1};
    s1.insert(7);
    s1.insert(5);

Accessing data in the set is done with an iterator, which is basically a pointer to the set.

    set<int> s1 = {2, 1, 3, 4, 0, 1};

    set<int>::iterator it = s1.begin();

    cout << *it << endl;
    it++;
    cout << *it << endl;
0
1

Multi-Set Container

The Multi-Set is almost an exact replica of the Set Container, with just one difference. Unlike the Set Container, Multi-set allows duplicates of an element to be stored in it’s container.

    multiset<int> ms1 = {4, 3, 4, 2, 1, 0 };

Other than this, the functions that you use with Set, can also be used with Multi-Set.


Map Container

What makes the Map Container different from other Associative Containers is that it stores data in the form key-value pairs. The Key is sort of like the Identification feature for the value, used to access and retrieve values from the Map. Keys must be unique, while values may be non-unique.

If you’ve ever used Dictionaries in Python, Maps in C++ are pretty much the same thing.

Here’s some code that has the basic implementation for a Map Container.

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<int, string> m1;

    m1.insert(pair<int, string> (1, "Apple"));
    m1.insert(pair<int, string> (2, "Banana"));
    m1.insert(pair<int, string> (3, "Orange"));

    map<int, string>::iterator it;

    for (it = m1.begin(); it != m1.end(); it++) {
        cout << "Key: " << it->first << "  Value: " << it->second << endl;
    }

    return 1;
}

Similar to the Set Container, we use insert() and pair to put data into the Map. Insert() actually inserts the data, while pair converts into the appropriate format.

To access it we use the iterator, and setup a for loop to iterate over all the Key-Value pairs in the map. We use the first attribute to return the Key, and the second attribute to retrieve the Value. The reason we are using -> is because the iterator is a pointer.


Multi-Map Container

A Multi-Map is just like a map, with one difference. They Key’s do not have to be unique, and can be duplicates of each other. However, the Value’s of these duplicate Key’s must be unique. In short, either the Key must be unique, or the Value. You cannot have two identical Key-Value pairs in a Multi-Map.

    multimap<int, string> mm1;

Other than this and a slight difference in the declaration, everything else works the same as regular maps.


This marks the end of the Associative Containers in C++ Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome at [email protected]. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments