Skip to content
Related Articles

Related Articles

Improve Article

set vs map in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 26 May, 2021

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;
}
Output: 
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;
}
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




// 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;
}
Output: 



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;
}
Output: 
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;
}
Output: 
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;
}
Output: 
Elements in unordered_map:
[ 3, 6]
[ 2, 3]
[ 8, 5]
[ 1, 2]
[ 4, 5]

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :