Why is std::set
defined as an associative container?
I mean std::map
is an associative container because it maps a value to a key, but why is it a set?
Why is std::set
defined as an associative container?
I mean std::map
is an associative container because it maps a value to a key, but why is it a set?
A
set
satisfies all of the requirements of [..] an associative container (23.2.4) [...]
Because it satisfies all pre-conditions of being an associative container, which are described in 23.2.4.
and aren't as simple as "maps a key to a value".
The second paragraph even highlights this (or rather, highlights that it is in fact map
and multimap
have additional functionality over associative containers):
2) Each associative container is parameterized on Key and an ordering relation Compare that induces a strict weak ordering (25.4) on elements of Key. In addition, map and multimap associate an arbitrary type T with the Key. The object of type Compare is called the comparison object of a container.
The full paragraph is too large to reproduce here.
In a set, the key is the value, which must be unique.
Edit:
"Elements in associative containers are referenced by their key and not by their absolute position in the container."