11

I've gone through various texts. The only thing I got is that set is an associative container consisting of sorted and unique keys. Now if there are no values to map using key so where is the association in the set.

cbinder
  • 2,388
  • 2
  • 21
  • 36
  • 1
    Interesting question, I never thought of it that way before. What are the entries in set associated with?? Their own existence I suppose. – Neil Kirk Aug 01 '14 at 01:02
  • Maybe for a `set`, the key and value are the same? – PaulMcKenzie Aug 01 '14 at 01:05
  • @PaulMcKenzie Don't you like `void`? – Deduplicator Aug 01 '14 at 01:12
  • I like to think that the "association" is between a container element and its value. By contrast, in sequence containers, elements are identified by their insertion order. – Kerrek SB Aug 01 '14 at 01:26
  • @Kerrek SB thats what I am thinking...and internally it is stored as a balanced BST taking logn time to search...so I guess it is just a BST representation of a list just to to get search time of logn – cbinder Aug 01 '14 at 01:40
  • @cbinder: I think that thinking about the implementation is misleading and doesn't help. The point is that in an associative container, container membership is determined by *value*, and in sequence containers by *position*. – Kerrek SB Aug 01 '14 at 08:38

4 Answers4

11

A Container is an object used to store other objects and taking care of the management of the memory used by the objects it contains.

An AssociativeContainer is an ordered Container that provides fast lookup of objects based on keys.

std::set is an associative container that contains a sorted set of unique objects of type Key

So what makes it associative? The fact that elements in a set are referenced by their key and not by their absolute position in the container. The key, of course, is the element itself. Think of it as a map where the keys are values are equal and given that, where the duplicate copy of the same content is eliminated.

So what about an unordered set then? std::unordered_set meets the requirements of Container, AllocatorAwareContainer and UnorderedAssociativeContainer

Carl
  • 43,122
  • 10
  • 80
  • 104
  • Lookup time for set and map is logarithmic, which lets us infer that it is a tree structure. Lookup time for the unordered equivalents is constant, which lets us infer that it is a hash-table. So, ya, the difference between map and set is whether or not the value is the key or you provide a key and a value. – Carl Aug 01 '14 at 01:41
1

There are many different way to think about it, and some of them often they lead to a typical chicken-or-egg dilemma.

Take std::map, for example, which by its interface specification is a straightforward associative container. Yet you can think of (and implement) std::map as a std::set of pairs key:data, with comparator function taking into account only the key portion of stored elements and ignoring the data portion. From that point of view, a "set" (std::set or std::unordered_set) can be seen as a more universal and more fundamental data structure than "map" (std::map or std::unordered_map). I.e. the functionality of "set" covers the functionality of a typical associative container storing key:data pairs. In other words, "set" is a father of associative containers and, for that reason alone, can be thought of as an associative container itself.

Of course, one can argue that one can use a "map" to easily implement a "set" (by using the same value as key and data), meaning that "map" can be seen as a more fundamental data structure than "set". This is the chicken-or-egg situation I was referring to before.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

If you consider a set to be an independent data structure in its own right, it does not fit the definition of an associative data structure because there is no mapped value. However, when a set is considered to be a special case of a map, with the mapped ("associated") value being the same as the key, it is fit to be termed as an associative container. Do note that a map is considered more of a primary data structure than a set because one can use a map to implement a set, but not vice versa.

Srikanth
  • 973
  • 2
  • 9
  • 19
0

We may assume there are two types of containers, one is linked list and another is associative container. We can pick value of any associative container by a key, for example in a simple array you can pick any value by its position, i.e. arr[position] = value. Here, position is key. But, you can't do this for a linked list.

Sohag
  • 11
  • 2