Associative Containers

An associative container is a type of container provided by the Standard Template Library (STL) that stores elements in a sorted order based on keys.

Instead of accessing elements by indexes like in sequence containers, associative containers use keys to access and organize elements.

These containers provide fast lookup times for elements and are ideal for scenarios where efficient searching and retrieval of data are critical.

Types of Associative Containers in STL:


There are four main types of associative containers in the C++ STL:

std::set:

  • std::set is an ordered set of unique keys, where each key corresponds to a unique element in the container.
  • It automatically maintains the sorted order of elements based on the keys, using a balanced binary search tree (usually a Red-Black Tree) for efficient searching, insertion, and deletion.
  • std::set does not allow duplicate elements.

std::multiset:

  • std::multiset is similar to std::set, but it allows duplicate keys and stores them in sorted order.
  • It is useful when you need to store multiple occurrences of the same key.

std::map:

  • std::map is an ordered collection of key-value pairs, where each key maps to a unique value.
  • Like std::set, std::map uses a balanced binary search tree to maintain the sorted order of keys, enabling efficient lookups, insertions, and deletions.
  • std::map does not allow duplicate keys.

std::multimap:

  • std::multimap is similar to std::map, but it allows multiple key-value pairs with the same key, all sorted by keys.
  • It is useful when you need to associate multiple values with a single key.

Example of Associative Containers:


Let’s demonstrate the usage of std::set, an associative container, to store and retrieve a collection of integers.

Code:

#include <iostream>
#include <set>

int main() {
    // Create a set to store integers
    std::set<int> mySet;

    // Insert elements into the set
    mySet.insert(30);
    mySet.insert(10);
    mySet.insert(50);
    mySet.insert(20);

    // Print the elements of the set (in sorted order)
    std::cout << "Elements in the set:" << std::endl;
    for (const int& num : mySet) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // Search for an element in the set
    int key = 20;
    auto searchResult = mySet.find(key);
    if (searchResult != mySet.end()) {
        std::cout << key << " found in the set." << std::endl;
    } else {
        std::cout << key << " not found in the set." << std::endl;
    }

    // Erase an element from the set
    int elementToRemove = 10;
    mySet.erase(elementToRemove);

    // Print the elements after removal
    std::cout << "Elements in the set after erasing " << elementToRemove << ":" << std::endl;
    for (const int& num : mySet) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

Output:

Elements in the set:
10 20 30 50
20 found in the set.
Elements in the set after erasing 10:
20 30 50

Explanation of the Code:
In this example, we use the std::set associative container from the STL:

  1. We create a set called mySet, which can store integers (int).
  2. We insert elements into the set using the insert method. The set automatically maintains the sorted order of elements based on their values.
  3. We iterate through the set using a range-based for loop and display its elements. The elements are displayed in sorted order, as guaranteed by the std::set.
  4. We search for an element with the value 20 in the set using the find method. If the element is found, we print a message indicating its presence; otherwise, we indicate that it is not found.
  5. We erase the element with the value 10 from the set using the erase method. After removal, the set is displayed again to show the updated elements.

Applications of Associative Containers:


Associative containers are well-suited for scenarios where efficient searching and retrieval of data based on keys are required, such as:

  • Maintaining unique elements in sorted order (std::set and std::map).
  • Storing and retrieving key-value pairs (std::map and std::multimap).
  • Handling data that requires quick access and lookups.

Pros and Cons of Associative Containers:
Pros:

  • Efficient lookup: Associative containers provide fast search times (usually logarithmic) for elements based on keys.
  • Sorted order: The elements are automatically maintained in sorted order, providing a predictable and organized data structure.
  • Flexibility: Associative containers offer different variations, such as allowing duplicates (std::multiset and std::multimap), giving developers more choices.

Cons:

  • Overhead: Associative containers may have higher memory overhead compared to sequence containers due to the need for additional bookkeeping for sorting and tree structures.
  • Slower insertion and deletion: Associative containers might have slower insertion and deletion times compared to sequence containers, especially for large datasets and tree-based implementations.