VOOZH about

URL: https://www.geeksforgeeks.org/cpp/difference-between-set-multiset-unordered_set-unordered_multiset-in-cpp/

⇱ Difference Between set, multiset, unordered_set, unordered_multiset in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 26 Feb, 2024

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.


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.


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.


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.


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