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

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

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

I'm having trouble finding the distinction.

Recommended Answers

All 5 Replies

In a set, the value is the key. In a map, the key and value are distinct.

So is a set like any other list/vector except that it's sorted?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.