Associative containers (C++)
| C++ Standard Library |
|---|
| Containers |
| C standard library |
In C++, associative containers or associative collections are a group of class templates in the standard library that implement ordered associative arrays. Being templates, they can be used to store arbitrary elements, such as integers or custom classes. Like all other standard library components, they reside in namespace std.
The following containers are defined in the current revision of the C++ standard:
std::set<T>std::map<K, V>std::multiset<T>std::multimap<K, V>
Each of these containers differ only on constraints placed on their elements.
There are also versions of these collections in namespace std::pmr (for polymorphic memory resources). These versions specify the optional template parameter Allocator as std::pmr::polymorphic_allocator.
std::set and std::multiset are declared in header <set>, while std::map and std::multimap are declared in header <map>.
The associative containers are similar to the unordered associative containers in C++ standard library, the only difference is that the unordered associative containers, as their name implies, do not order their elements.
std::map and std::set are usually implemented as red-black trees, and are essentially (respectively) equivalent to java.util.TreeMap and java.util.TreeSet from Java or std::collections::BTreeMap and std::collections::BTreeSet from Rust.