Does std::set store objects contiguously in memory?
Does std::set store objects in contiguous memory like std::vector?
There is no guarantee that it does. Also in practice, it cannot because of the requirements of the container. Therefore no, it does not store objects in contiguous memory.
I can't see why it couldn't use contiguous memory
References to elements of the set must remain valid upon insertion to it as well as erasure (except for references to the erased element). This requirement is incompatible with contiguous memory.
As far as I know, a balanced search tree is the only data structure that can implement std::set
.
It isnt excluded explicitly, though certain constraints for std::set
make it impossible to use contiguous memory.
For example, set::insert
has logarithmic complexity while vector::insert
requires linear complexity to shuffle its entries. Also set::insert
does not invalidate iterators. Both requirements cannot be realized with continguous memory.