Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between set vs map in C++?

Tags:

c++

stl

I am still confused by the differences between the map and set datastructures in STL. I know set is storing the values in a sorted way, what about map? Does it store the values in sorted order? Map stores pairs of values (key,value), what is the advantage of this feature?

like image 396
SheikCode Avatar asked Feb 28 '14 07:02

SheikCode


People also ask

What is the difference between map and set?

The main difference between Set and Map is that Set contains only data elements, and the Map contains the data in the key-value pair, so Map contains key and its value.

Which is faster set or map?

The map solution results in "Time Limit Exceeded on Test 3", whereas the set solution results in "Time Limit Exceeded on Test 2", which means that Test 2 is such that the map solution works faster on it than the set solution.

Why are sets and maps useful?

Sets are used to get information of an object by providing all the information, usually used to check if the data exists. A map is used to get the information of an object by using a key (single data).

What is the difference between map and pair?

A pair is a single unit with two members. A map has keys and values in it. So you can use pairs to fill up a map, the elements of the pair becoming key and value.

What is the difference between a set and a map?

Note that the time complexities of search, insert and delete are O (Log n). The difference is set is used to store only keys while map is used to store key value pairs. For example consider in the problem of printing sorted distinct elements, we use set as there is value needed for a key.

What is set vs map in C++ STL?

Set vs Map in C++ STL. Set is an abstract data type in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, but it is possible to remove and add the modified value of that element.

What is the difference between set and map in DBMS?

Differences: The difference is set is used to store only keys while map is used to store key value pairs. For example consider in the problem of printing sorted distinct elements, we use set as there is value needed for a key. While if we change the problem to print frequencies of distinct sorted elements, we use map.

What is the use of map in C++?

At least for the ordered versions (std::map and std::set), a map facilitates use-cases of a set by allowing you to introduce an external key (map::key_type) to determine ordering of the elements that otherwise can't be derived from map's data type (map::data_type).


4 Answers

At least for the ordered versions (std::map and std::set), a map facilitates use-cases of a set by allowing you to introduce an external key (map::key_type) to determine ordering of the elements that otherwise can't be derived from map's data type (map::mapped_type). If the ordering can be wholly derived (by comparing 2 elements) from map::mapped_type, then you're typically better off using a set, in which case you'll avoid duplicating the key as map::key_type.

In a way, std::map is redundant and you can always use std::set instead by introducing a new element type which aggregates keys with data while providing the necessary comparison function. However, this is cumbersome and typically inelegant.

To clarify why a set may be cumbersome over a map; A set will store the <key, data> pair as an element while map will maintain a separation between the 2. This means, for instance, that for a find operation on a set where find's parameter is constructed on-the-spot, an entire <key, data> element will have to be constructed while it's really on the key that's needed for the find operation. The construction of the data members of a set's element is then redundant, and can be rather inefficient if, for instance, data members represent disk storage or involve some other complex or else time consuming construction operation. map alleviates this by only having to construct the actual key required for find.

To summarize, consider an element <key, data> for which you're wondering whether to use a map or a set to store multiple ordered elements. If key spans the entire data (meaning data is empty or else key == data) then you're better off using a set in which case you'll avoid a) duplicating key storage and b) likely having to keep 2 keys synchronized. If key is not contained in data then (you have to) use a map. The tricky part is when key is a (proper) subset of data. You then have to trade-off the cost of maintaining duplicate keys (for a map) vs the cost of constructing data that doesn't overlap with key (for a set), the latter which may occur for find operations.

like image 180
Vijay Rao Avatar answered Oct 19 '22 01:10

Vijay Rao


Conceptually, a set is a collection of things, whereas a map is a mapping of keys to values.

like image 39
NPE Avatar answered Oct 19 '22 01:10

NPE


A map stores keys sorted. It maps keys to values. Usually it is implemented as a binary search tree (red-black tree) for keys. A set is a map where values are irrelevant. unordered_map and unordered_set (new in C++11) store keys unsorted and use hash table for search.

like image 13
Spock77 Avatar answered Oct 19 '22 00:10

Spock77


std::map and std::set are extremely similar. They both have a sorted collection of unique keys. Additionally, map has a value associated with each key.

like image 2
SzG Avatar answered Oct 19 '22 00:10

SzG