Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference Between set, multiset, unordered_set, unordered_multiset in C++

  • Difficulty Level : Easy
  • Last Updated : 28 Apr, 2022

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. SetThey 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




// CPP program to demonstrate insert and
// delete in set
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    // set declare
    set<int> s;
 
    // Elements added to set
    s.insert(12);
    s.insert(10);
    s.insert(2);
    // duplicate added
    s.insert(10);
    s.insert(90);
    s.insert(85);
    s.insert(45);
 
    // Iterator declared to traverse
    // set elements
    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);
 
    // elements from 10 to elements before
    // 90 erased
    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. MultisetThey 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




// CPP program to demonstrate insert and
// delete in set
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
    // multiset declare
    multiset<int> s;
 
    // Elements added to set
    s.insert(12);
    s.insert(10);
    s.insert(2);
    // duplicate added
    s.insert(10);
    s.insert(90);
    s.insert(85);
    s.insert(45);
 
    // Iterator declared to traverse
    // set elements
    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);
 
    // elements from 10 to elements before 90
    // erased
    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: They 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

 

Syntax:

unordered_set<datatype> setname;

The following example demonstrates the application of Unordered set.

CPP




// CPP program to demonstrate insert and
// delete in unordered_set
#include <bits/stdc++.h>
using namespace std;
int main()
{
    // unordered_set declare
    unordered_set<int> s;
 
    // Elements added to set
    s.insert(12);
    s.insert(10);
    s.insert(2);
    // duplicate added
    s.insert(10);
    s.insert(90);
    s.insert(85);
    s.insert(45);
    s.insert(12);
    s.insert(70);
 
    // Iterator declared to traverse
    // set elements
    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);
 
    // element 10 erased
    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: It 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




// CPP program to demonstrate insert and
// delete in unordered_multiset
#include <bits/stdc++.h>
using namespace std;
int main()
{
    // unordered_multiset declare
    unordered_multiset<int> s;
 
    // Elements added to set
    s.insert(12);
    s.insert(10);
    s.insert(2);
    // duplicate added
    s.insert(10);
    s.insert(90);
    s.insert(85);
    s.insert(45);
 
    // Iterator declared to traverse
    // set elements
    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);
 
    // element 10 trained
    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.

This article is contributed by SHAURYA UPPAL. 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. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!