set vs map in C++ STL

13 Jun, 2022
set and map in STL are similar in the sense that they both use Red Black Tree (A self balancing BST). Note that the time complexities of search, insert and delete are O(Log n). 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. We need map to store array values as key and frequencies as value. 
CPP
// CPP program to demonstrate working of set#include <bits/stdc++.h>using namespace std; int main(){    set<int> s1;    s1.insert(2);    s1.insert(5);    s1.insert(3);    s1.insert(6);     cout << "Elements in set:\n";    for (auto it : s1)        cout << it << " "; // Sorted     return 0;}
Elements in set:
2 3 5 6
 
CPP
// CPP program to demonstrate working of map#include <bits/stdc++.h>using namespace std; int main(){    map<int, int> m;     m[1] = 2; // Insertion by indexing     // Direct pair insertion    m.insert({ 4, 5 });     // Insertion of pair by make_pair    m.insert(make_pair(8, 5));     cout << "Elements in map:\n";    for (auto it : m)        cout << "[ " << it.first << ", "             << it.second << "]\n"; // Sorted     return 0;}
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/unordered_map. Multimap: Multimap doesn’t allow elements to stored by indexing.  
CPP
// CPP program to demonstrate working of Multimap#include <bits/stdc++.h>using namespace std; int main(){    multimap<int, int> m;     m.insert({ 1, 2 });    m.insert({ 2, 3 });    m.insert({ 4, 5 });    m.insert({ 2, 3 });    m.insert({ 1, 2 });     cout << "Elements in Multimap:\n";    for (auto it : m)        cout << "[ " << it.first << ", "             << it.second << "]\n"; // Sorted     return 0;}
Elements in Multimap:
[ 1, 2]
[ 1, 2]
[ 2, 3]
[ 2, 3]
[ 4, 5]
 
Multiset:  
CPP
// CPP program to demonstrate working of Multiset#include <bits/stdc++.h>using namespace std; int main(){    multiset<int> ms;     ms.insert(1);    ms.insert(3);    ms.insert(4);    ms.insert(2);    ms.insert(2);     cout << "Elements in Multiset:\n";    for (auto it : ms)        cout << it << " ";     return 0;}
Elements in Multiset:
1 2 2 3 4
 
Unordered_set:  
CPP
// CPP program to demonstrate working of Unordered_set#include <bits/stdc++.h>using namespace std; int main(){    unordered_set<int> us;     us.insert(1);    us.insert(3);    us.insert(4);    us.insert(2);    us.insert(2);     cout << "Elements in unordered_set:\n";    for (auto it : us)        cout << it << " "; // Sorted     return 0;}
Elements in unordered_set:
2 4 1 3
 
Unordered_map:  
CPP
// CPP program to demonstrate working of Unordered_map#include <bits/stdc++.h>using namespace std; int main(){    unordered_map<int, int> um;     um[1] = 2;    um[4] = 5;    um[2] = 3;    um[8] = 5;    um[3] = 6;     cout << "Elements in unordered_map:\n";    for (auto it : um)        cout << "[ " << it.first << ", " << it.second << "]\n";     return 0;}
Elements in unordered_map:
[ 3, 6]
[ 2, 3]
[ 8, 5]
[ 1, 2]
[ 4, 5]
 
Let us see the differences in a tabular form -: 
Its syntax is -:
set<data_type>name_of_set;
Its syntax is -:
map<data_type , data_type>name_of_map;
hansschukkink
surajv
mayank007rawa
cpp-map
cpp-set
STL
C++
Difference Between
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bitwise Operators in C/C++
vector erase() and clear() in C++
Substring in C++
Priority Queue in C++ Standard Template Library (STL)
Inheritance in C++
Class method vs Static method in Python
Difference between BFS and DFS
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Differences between JDK, JRE and JVM