
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Set, Multiset, Unordered Set, and Unordered Multiset in C++
Here we will see what are the differences for set, multiset, unordered_set and unordered_multiset in C++. Let us see the properties of them using some example.
Set
The properties of set are like below
- Stores data in sorted order
- Stores only unique values
- We can insert or delete data, but cannot change the data
- We can remove more than one element using start and end iterator
- We can traverse using iterators
- Sets are implemented using Binary Search Tree
Now let us see an example
Example
#include <iostream> #include <set> using namespace std; main() { int data[15] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41}; set<int> my_set; for(int i = 0; i<15; i++) { my_set.insert(data[i]); } set<int>::iterator it; for(it = my_set.begin(); it != my_set.end(); it++) { cout << "Item: " << *it << endl; } }
Output
Item: 11 Item: 22 Item: 23 Item: 33 Item: 41 Item: 44 Item: 55 Item: 66 Item: 77 Item: 88 Item: 99
Multiset
The properties of set are like below −
- Stores data in sorted order
- It allows to store duplicate data
- We can remove more than one element using start and end iterator.
Now let us see an example.
Example
#include <iostream> #include <set> using namespace std; main() { int data[15] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41}; multiset<int> my_set; for(int i = 0; i<15; i++) { my_set.insert(data[i]); } multiset<int>::iterator it; for(it = my_set.begin(); it != my_set.end(); it++) { cout << "Item: " << *it << endl; } }
Output
Item: 11 Item: 11 Item: 22 Item: 22 Item: 23 Item: 33 Item: 41 Item: 44 Item: 55 Item: 66 Item: 66 Item: 66 Item: 77 Item: 88 Item: 99
Unordered Set
The properties of set are like below −
- Data can be placed in any order
- Duplicate data will be discarded
- This set is created using Hash-tables
- We can erase only one element, where the iterator is pointing
Now let us see an example.
Example
#include <iostream> #include <unordered_set> using namespace std; main() { int data[15] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41}; unordered_set<int> my_set; for(int i = 0; i<15; i++) { my_set.insert(data[i]); } unordered_set<int>::iterator it; for(it = my_set.begin(); it != my_set.end(); it++) { cout << "Item: " << *it << endl; } }
Output
Item: 11 Item: 55 Item: 22 Item: 66 Item: 33 Item: 44 Item: 77 Item: 88 Item: 99 Item: 23 Item: 41
Unordered Multiset
The properties of set are like below −
- Data can be placed in any order
- Duplicate data are allowed
- This set is created using Hash-tables
- We can erase only one element, where the iterator is pointing
Now let us see an example.
Example
#include <iostream> #include <unordered_set> using namespace std; main() { int data[15] = {11, 55, 22, 66, 33, 22, 11, 44, 77, 88, 66, 99, 66, 23, 41}; unordered_multiset<int> my_set; for(int i = 0; i<15; i++) { my_set.insert(data[i]); } unordered_multiset<int>::iterator it; for(it = my_set.begin(); it != my_set.end(); it++) { cout << "Item: " << *it << endl; } }
Output
Item: 11 Item: 55 Item: 22 Item: 66 Item: 33 Item: 22 Item: 11 Item: 44 Item: 77 Item: 88 Item: 66 Item: 99 Item: 66 Item: 23 Item: 41
Advertisements