Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why std::set is an associative container

Tags:

c++

stl

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.

like image 841
cbinder Avatar asked Aug 01 '14 00:08

cbinder


1 Answers

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

like image 111
Carl Avatar answered Oct 09 '22 14:10

Carl