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]
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count of prime factors of N to be added at each step to convert N to M
- Most frequent element in Array after replacing given index by K for Q queries
- Difference between Sentinel and Counter Controlled Loop in C
- Difference between Puppeteer and Selenium
- Difference between Trafodion and Virtuoso
- Difference between Hive and Oracle
- Difference between Impala and dBASE
- Difference between Derby and Cassandra
- Difference between InfluxDB and Microsoft Access
- Difference between Elasticsearch and Solr
- Difference between Android 1.0 and Android 1.1
- Difference between Derby and Pouchdb
- Difference between Derby and PostgreSQL
- Difference between Google Hangouts and Skype
- Difference between Google Voice and Skype
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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