In C++ Standard Template Library, set, multiset, unordered_set, unordered_multiset are used to store elements. Although they are similar but differ from each other in some functionalities. The differences are discussed below:
1. Set: Sets are associative containers that store unique elements following a specific order. Following are the properties of sets:
- Stores the values in sorted order.
- Stores only unique values.
- Elements can only be inserted or deleted but cannot be modified.
- We can erase more than 1 element by giving the start iterator and end iterator position.
- Traversal using iterators.
- Sets are implemented as Binary Search Tree.
Syntax:
set<datatype> setname;
The following example demonstrates the application of set.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
set< int > s;
s.insert(12);
s.insert(10);
s.insert(2);
s.insert(10);
s.insert(90);
s.insert(85);
s.insert(45);
set< int >::iterator it, it1, it2;
cout << "Set elements after sort and "
"removing duplicates:\n" ;
for (it = s.begin(); it != s.end(); it++)
cout << *it << ' ' ;
cout << '\n' ;
it1 = s.find(10);
it2 = s.find(90);
s.erase(it1, it2);
cout << "Set Elements after erase:\n" ;
for (it = s.begin(); it != s.end(); it++)
cout << *it << ' ' ;
return 0;
}
|
Output
Set elements after sort and removing duplicates:
2 10 12 45 85 90
Set Elements after erase:
2 90
2. Multisets: Multisets are associative containers that store multiple elements having equivalent values following a specific order. Following are the properties of multisets:
- Stores elements in sorted order.
- It allows the storage of multiple elements.
- We can erase more than 1 element by giving the start iterator and end iterator.
Note: All other properties are similar to the set.
Syntax:
multiset<datatype> multisetName;
The following example demonstrates the application of Multiset.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset< int > s;
s.insert(12);
s.insert(10);
s.insert(2);
s.insert(10);
s.insert(90);
s.insert(85);
s.insert(45);
multiset< int >::iterator it, it1, it2;
cout << "Multiset elements after sort\n" ;
for (it = s.begin(); it != s.end(); it++)
cout << *it << ' ' ;
cout << '\n' ;
it1 = s.find(10);
it2 = s.find(90);
s.erase(it1, it2);
cout << "Multiset Elements after erase:\n" ;
for (it = s.begin(); it != s.end(); it++)
cout << *it << ' ' ;
return 0;
}
|
Output
Multiset elements after sort
2 10 10 12 45 85 90
Multiset Elements after erase:
2 90
3. unordered_set: unordered_set are associative containers that store unique elements in no particular order. Following are the properties of Unordered_sets:
- Elements can be stored in any order. ( no sorted order )
- Stores only unique values.
- Hash-table used to store elements.
- We can erase only the element for which the iterator position is given.
Note: All other properties are similar to the set.
Syntax:
unordered_set<datatype> setname;
The following example demonstrates the application of Unordered set.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_set< int > s;
s.insert(12);
s.insert(10);
s.insert(2);
s.insert(10);
s.insert(90);
s.insert(85);
s.insert(45);
s.insert(12);
s.insert(70);
unordered_set< int >::iterator it, it1;
cout << "Unordered_set elements after sort:\n" ;
for (it = s.begin(); it != s.end(); it++)
cout << *it << ' ' ;
cout << '\n' ;
it1 = s.find(10);
s.erase(it1);
cout << "Unordered_set Elements after erase:\n" ;
for (it = s.begin(); it != s.end(); it++)
cout << *it << ' ' ;
return 0;
}
|
Output
Unordered_set elements after sort:
10 45 12 70 2 90 85
Unordered_set Elements after erase:
45 12 70 2 90 85
4. Unordered_multiset: Unordered_multiset is an associative container that contains a set of non-unique elements in unsorted order. Following are the properties of Unordered_multiset:
- Elements can be stored in any order.
- Duplicate elements can be stored.
- Hash-table used to store elements.
- We can erase only the element for which the iterator position is given.
Note: All other properties are similar to the set.
Syntax:
unordered_multiset<datatype> multisetName;
The following example demonstrates the application of Unordered multiset.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_multiset< int > s;
s.insert(12);
s.insert(10);
s.insert(2);
s.insert(10);
s.insert(90);
s.insert(85);
s.insert(45);
unordered_multiset< int >::iterator it, it1;
cout << "Unordered-Multiset elements after sort:\n" ;
for (it = s.begin(); it != s.end(); it++)
cout << *it << ' ' ;
cout << '\n' ;
it1 = s.find(10);
s.erase(it1);
cout << "Unordered-Multiset Elements after "
"erase:\n" ;
for (it = s.begin(); it != s.end(); it++)
cout << *it << ' ' ;
return 0;
}
|
Output
Unordered-Multiset elements after sort:
45 10 10 12 2 90 85
Unordered-Multiset Elements after erase:
45 10 12 2 90 85
Difference between set, multiset, unordered_set, unordered_multiset:
- In simple words, set is a container that stores sorted and unique elements. If unordered is added means elements are not sorted.
- If multiset is added means duplicate elements storage is allowed.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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!
Last Updated :
09 Nov, 2022
Like Article
Save Article