set vs map in C++ STL - GeeksforGeeks (2024)

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]

Let us see the differences in a tabular form -:

setmap
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 orderIt 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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated : 10 Nov, 2022

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

set vs map in C++ STL - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Jerrold Considine

Last Updated:

Views: 6204

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.