Associative Containers In C++
The Standard Template Library (STL) in C++ supports associative containers, a sort of data structure that lets you maintain and access components through keys. Associative containers utilise keys to efficiently arrange and access elements as opposed to sequential containers like vectors or lists, where components are maintained in a linear sequence.
In C++, there are four different categories of associative containers.
1.std::map : An ordered associative container in C++ is called std::map. The elements are organised in ascending order according to the keys and are stored as key-value pairs with unique keys. The independent binary search tree that is used to construct it (usually a red-black tree) guarantees that insertion, removal, and retrieval operations take logarithmic time to complete. This container comes in handy should you need to easily do key-based lookups while still keeping a large amount of data in sorted order.
2.std::multimap : Std::multimap is an organised associative container that holds key-value pairs and is similar to std::map in that regard. However, std::multimap permits duplicate keys, in contrast to std::map. The keys continue to determine the elements' sequence. When you need to keep the sorting order while associating multiple numbers with the same key, this container comes in handy.
3. std::set: A collection of distinct values can be stored in an ordered associative container called std::set. The elements are kept in an independent binary search tree, typically a red-black tree, which guarantees that inserting, deleting, and retrieving data takes a logarithmic amount of time. It is appropriate when you need to keep an organised set of unique values and run quick participation checks.
4.std::multiset: Std::multiset represents an order container that houses a collection of values, much as std::set. However, duplicate elements are supported by std::multiset. The component parts are still arranged despite duplicates. This container can be useful if it's required to save several copies of identical data while retaining order.
5.std::unordered_map: The keys and their values are stored in the associative container std::unordered_map, which is not sorted. It achieves the typical constant-time complexity for inserting, deleting, and retrieval tasks through the use of hashing. The proper arrangement of the components is not assured. When ordering is unimportant and you need quick key-based lookups, it can be helpful.
6.std::multimap_map : Std::unordered_multimap is an unsorted container that allows duplicate keys, similar to Std::unordered_map. According to their hashed values, components are arranged in an unordered fashion. It is appropriate for situations where you must unorderedly link several values to the same key.
7.std::unordered_set: The unsorted associative container std::unordered_set is used to hold a collection of distinct values. When performing insertion, removal, and retrieval operations, elements are hashed to determine their typical constant-time complexity. It is effective when you require brief participation tests since there is uncertainty surrounding the sequencing of the components.
8.std::unordered_multiset: Comparable to std::unordered_set, std::unordered multiset is an unordered package that accepts an extreme amount of elements. It keeps properties that are compatible with their later hashed values. This container can be useful if you need to preserve various alternatives of similar data on a continuous basis.
Example:
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> Ages;
Ages [1] = "Ramesh";
Ages [2] = "Suresh";
Age [3] = "Rajesh";
//Accessing elements
std::cout << Ages[3] << std::endl; // Output: "Rajesh"
//Iterating through the map
for (const auto& pair: Ages) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}
Output:
![Associative Containers In C++](https://static.tutorialandexample.com/cpp/associative-containers-in-cpp1.png)
Working with Associative Containers:
1. Declaration and initialization:
You must define the instance of the container's class with the necessary template arguments in order to generate an associative container. For instance, to make a std::map with integer values and string keys:
#include <map>
std::map<std::string, int> ageMap;
2.Inserting elements:
A variety of techniques can be used to add components to an associative container. Using the insert() function, for instance:
ageMap.insert(std::make_pair("Bob", 25));
3.Accessing Elements:
The superscript operator [], which is accessible for various containers like std::map and std::unordered_map, can be used to obtain elements from the container:
int aliceAge = ageMap["Bob"]; // Retrieves the age associated with the key Alice."
4.Using iterators:
You can cycle through the components of an associating container using iterators. Here is an illustration of how to use std: map to display an iterator:
for (auto it =Map.begin(); it!= Map.end(); ++it) {
std::cout << "name: " << it->first << ", Age: " <<it->second << std::endl;
}
5. Finding and Erasing Elements:
The functions find() and erase() can be used to locate and remove elements, respectively. For instance:
auto it = ageMap.find("Bob");
if (it!= ageMap.end()) {
ageMap.erase(it); // Removes the element with key "Bob"
}
Conclusion:
As a result, C++'s associative containers are flexible and strong tools for quickly arranging, retrieving, and modifying data based on keys. Both the ordered and the unordered versions of these containers have their own advantages, depending on the demands of your undertaking.