14

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?

hmjd
  • 120,187
  • 20
  • 207
  • 252
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108
  • 2
    @LuchianGrigore 23.4 and elsewhere. – ecatmur Apr 02 '13 at 10:03
  • 1
    It associates itself to itself... – Alex Chamberlain Apr 02 '13 at 10:08
  • The distinction in the standard is "sequence container" versus "associative container". While it's a bit of a stretch for a set to be "associative", it's a reasonable choice for that contrast in names. – Pete Becker Apr 02 '13 at 11:43
  • Does this answer your question? [Why std::set is an associative container](https://stackoverflow.com/questions/25071335/why-stdset-is-an-associative-container) – Alex May 14 '22 at 05:35

2 Answers2

6

23.4.6.1 Class template set overview [set.overview]

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):

23.2.4 Associative containers [associative.reqmts]

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Got it, so it's not the key-value associativity that defines a container – Johnny Pauling Apr 02 '13 at 10:20
  • @Luchian Can we say std::set is container that is just like a sequential container with additional functionalities like sorted data and implementation in the form of red-black tree to provide O(logn) access – cbinder Jul 23 '14 at 07:56
  • @cbinder I don't think the RB tree implementation is required. – Luchian Grigore Jul 23 '14 at 08:14
  • have a look here http://stackoverflow.com/questions/10375334/what-kind-of-tree-implementation-is-stl-set and also do note on my earlier comment whether that is reasonable to say or not. – cbinder Jul 23 '14 at 08:59
2

reference at cplusplus.com

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."

jt234
  • 662
  • 6
  • 16