Associative Containers In C++ - C++ Programming Tutorial
C++ Course / STL Basics / Associative Containers In C++

Associative Containers In C++

BLUF: Mastering Associative Containers In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Associative Containers In C++

C++ is renowned for its efficiency. Learn how Associative Containers In C++ enables low-level control and high-performance computing in the tutorial below.

The C++ Standard Library provides a diverse range of effective containers. These containers essentially represent different forms of storage data structures. There are also alternative variations like algorithm and iterator implementations based on templates within the Standard Library. Nevertheless, the primary purpose of containers is to store elements. Templating the container is necessary for flexibility, allowing it to accommodate objects of various data types.

Each container comes equipped with a set of member functions, with most of them having a common set of function prototypes. This feature offers a valuable advantage to developers in quickly grasping the functionality based on the function name and knowing how to leverage them, regardless of the specific type of container being used. This discussion focuses specifically on ordered associative containers.

C++ offers a wide range of Containers, each serving specific purposes. Among these, the Associative Containers stand out as a distinct category. Through this session, you will gain insights into the rationale behind labeling them as "Associative" Containers.

In C++, an associative collection holds "Ordered Data," unlike other types of collections. This characteristic leads to quicker search and retrieval operations. However, it also implies that inserting data will require additional time (to ensure proper ordering).

All container categories within C++ are designed as class templates, allowing them to hold a diverse range of data types. Furthermore, they make use of the identical declaration approach, which simplifies comprehension, memorization, and manipulation across various container types.

Set up the Container

The C++ Set Container stands out among other Associative Containers due to its storage of data that is both unique and sorted. When we say unique, we mean that this container does not allow any duplicate elements. Any redundant inputs will be promptly removed.

A concise code illustration is presented below to showcase the Set Container functioning.

C++ Program:

Example

#include <iostream>
#include <set>
using namespace std;
 
int main(){
    set<int> s01 = {2, 1, 3, 4, 0, 1};
    cout << "OUTPUT: " << s01.size() << endl;
 
    if (!s01.empty()){
        for (int i: s01){
            cout << i << endl;
        }
    }
}

Output:

Output

OUTPUT: 5
0
1
2
3
4

Container with Several Sets

With the exclusion of one difference, the Multi-Set closely resembles the Set Container. Unlike the Set Container, the Multi-set Container allows for the retention of duplicate elements within its container.

Example

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

Apart from that, the actions performed using Set are also applicable to Multi-Set.

Map Container

The Map Container distinguishes itself from other Associative Containers by storing information in key-value pairs. The Key functions similarly to the value's Identification attribute, allowing for the retrieval and access of values within the Map. Keys must be unique, while values can be repetitive.

If you have experience with Dictionaries in Python, you'll find that Maps in C++ share a close resemblance.

Here are some code examples utilizing the fundamental Map Container technique.

C++ Program:

Example

#include <iostream>
#include <map>
using namespace std;
 
int main(){
    map<int, string> m01;
 
    m01.insert(pair<int, string> (1, "Grapes"));
    m01.insert(pair<int, string> (2, "Guava"));
    m01.insert(pair<int, string> (3, "Watermelon"));
 
    map<int, string>::iterator it;
 
    for (it = m01.begin(); it != m01.end(); it++) {
        cout << "Key: " << it->first << "  Value: " << it->second << endl;
    }
 
    return 1;
}

Output:

Output

Key: 1  Value: Grapes
Key: 2  Value: Guava
Key: 3  Value: Watermelon

To add information into the Map, we utilize insert along with pair, similar to how it was done with the Set Container. The insert function adds the data, while pair formats it appropriately.

To access the content, we employ the iterator along with a for loop to iterate over all Key-Value pairs within the map data structure. The initial attribute is employed to retrieve the Key, whereas the subsequent attribute is utilized to fetch the corresponding Value. Given that the iterator is a pointer, we must utilize -> notation.

Multi-Map Container

A Multi-Map shares similarities with a regular map, with the key difference being that keys are not obligated to be unique; they can be repeated. However, the values corresponding to these duplicate keys must be unique. Essentially, either the key or the value must be unique. In a Multi-Map, it is not permissible to have two identical key-value pairs.

Example

multimap<int, string> mm1;

All remaining options function in a comparable manner to traditional maps, apart from a slight difference in how they are declared.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience