Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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 (Note : sorted "
            "and duplcayes removed) \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 (Note : sorted and duplcayes removed) 
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




// 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 (Note : Sorted and "
      " duplicates allowed) \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 (Note : Sorted and  duplicates allowed) 
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




// 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 (Note : "
           "Not sorted):\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 (Note : Not sorted):
70 85 45 12 10 2 90 
Unordered_set Elements after erase:
70 85 45 12 2 90 

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




// 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 (Note : Elements are "
      " not sorted and duplciates are allowed :\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 (Note : Elements are  not sorted and duplciates are allowed :
85 45 12 90 2 10 10 
Unordered-Multiset Elements after erase:
85 45 12 90 2 10 

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.

 



Last Updated : 26 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads