What's the difference between a set and a map? (As described below on cppreference)

The C++ Set is an associative container that contains a sorted set of unique objects.

C++ Maps are sorted associative containers that contain unique key/value pairs.

I'm having trouble finding the distinction.

13 Years
Discussion Span
Last Post by Narue

>So is a set like any other list/vector except that it's sorted?
Sure, you can think of it like that for the time being. :)


OK. By the way, is there some sort of parent class that all STL containers descend from? Let's say I have my own container class and in it's constructor I have a flag that says I want my container to be sorted or non sorted. If it's sorted, I use set, if it's not sorted, I use vector. (As an example). If so, I would have a pointer to this parent class, and then in the constructor, I would set the pointer = to the descendent class. Does this make sense?


>By the way, is there some sort of parent class that all STL containers descend from?
No. In fact, all of the standard containers are concrete classes and can't be safely used as base classes because they lack a virtual destructor.

>Does this make sense?
Sorted and not sorted are only minor details. For example, a set doesn't support random access iterators while a vector does. So your container's public interface would change drastically because non-sorted is a sequence container and sorted is an associative container. If you want a sorted vector then the standard sort algorithm can be used periodically, or you can create your own sorted sequence class that contains a vector and supports the operations that you need for it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.