set vs map in C++ STL
Last Updated :
10 Nov, 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
#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 << " " ;
return 0;
}
|
Output:
Elements in set:
2 3 5 6
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
map< int , int > m;
m[1] = 2;
m.insert({ 4, 5 });
m.insert(make_pair(8, 5));
cout << "Elements in map:\n" ;
for ( auto it : m)
cout << "[ " << it.first << ", "
<< it.second << "]\n" ;
return 0;
}
|
Output:
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
#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" ;
return 0;
}
|
Output:
Elements in Multimap:
[ 1, 2]
[ 1, 2]
[ 2, 3]
[ 2, 3]
[ 4, 5]
Multiset:
CPP
#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;
}
|
Output:
Elements in Multiset:
1 2 2 3 4
Unordered_set:
CPP
#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 << " " ;
return 0;
}
|
Output:
Elements in unordered_set:
2 4 1 3
Unordered_map:
CPP
#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;
}
|
Output:
Elements in unordered_map:
[ 3, 6]
[ 2, 3]
[ 8, 5]
[ 1, 2]
[ 4, 5]
Let us see the differences in a tabular form -:
|
set |
map |
1. |
Set is used to store all the unique elements. |
map is used to store all the unique elements. |
2. |
Its syntax is -:
set<data_type>name_of_set;
|
Its syntax is -:
map<data_type , data_type>name_of_map;
|
3. |
It stores the elements in increasing order |
It stores the elements in key , value pairs. |
4. |
Set is implemented using Binary search tree. |
Map is implemented using Balance Binary tree. |
5. |
Sets are traversed using the iterators. |
It is defined in #include <map> header file. |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...