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. We need map to store array values as key and frequencies as value.
Elements in set: 2 3 5 6
Elements in map: [ 1, 2] [ 4, 5] [ 8, 5]
Variations of set and map:
Set and Map, both stores unique values and sorted values as well. But If we don’t have such a requirement, we use multiset/multimap and unordered_set/unoredred_map.
Multimap: Multimap doesn’t allow elements to stored by indexing.
Elements in Multimap: [ 1, 2] [ 1, 2] [ 2, 3] [ 2, 3] [ 4, 5]
Elements in Multiset: 1 2 2 3 4
Elements in unordered_set: 2 4 1 3
Elements in unordered_map: [ 3, 6] [ 2, 3] [ 8, 5] [ 1, 2] [ 4, 5]
- return statement in C/C++ with Examples
- Difference between Public and Private in C++ with Example
- How to implement our own Vector Class in C++?
- Web Programming in C++
- Difference between SVG and JPEG
- Difference between Linear and Non-linear Data Structures
- C++ Program to print an Array using Recursion
- Storage Classes in C++ with Examples
- fill_n() function in C++ STL with examples
- fill() function in C++ STL with examples
- Difference between Tester and SDET
- Difference between Software Testing and Embedded Testing
- Difference between Mirroring and Replication
- How to erase an element from a vector using erase() and reverse_iterator?
- iomanip setbase() function in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : hansschukkink