| set | unordered_set --------------------------------------------------------- Ordering | increasing order | no ordering | (by default) | Implementation | Self balancing BST | Hash Table | like Red-Black Tree | search time | log(n) | O(1) -> Average | | O(n) -> Worst Case Insertion time | log(n) + Rebalance | Same as search Deletion time | log(n) + Rebalance | Same as search
Use set when
- We need ordered data.
- We would have to print/access the data (in sorted order).
- We need predecessor/successor of elements.
- Since set is ordered, we can use functions like binary_search(), lower_bound() and upper_bound() on set elements. These functions cannot be used on unordered_set().
- See advantages of BST over Hash Table for more cases.
Use unordered_set when
- We need to keep a set of distinct elements and no ordering is required.
- We need single element access i.e. no traversal.
set: Input : 1, 8, 2, 5, 3, 9 Output : 1, 2, 3, 5, 8, 9 Unordered_set: Input : 1, 8, 2, 5, 3, 9 Output : 9 3 1 8 2 5
If you want to look at implementation details of set and unordered_set in c++ STL, see Set Vs Map. Set allows to traverse elements in sorted order whereas Unordered_set doesn’t allow to traverse elements in sorted order.
Elements of set in sorted order: 1 2 3 5 6 7
Elements of unordered_set: 2 7 5 1 6 3
Predecessor/Successor in Set:
Set can be modified to find predecessor or successor whereas Unordered_set doesn’t allow to find predecessor/Successor.
predecessor of 5 is=2 No predecessor predecessor of 8 is=5 successor of 5 is=8 successor of 2 is=5 No successor
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- set::begin() and set::end() in C++ STL
- set::rbegin() and set::rend() in C++ STL
- Count number of unique Triangles using STL | Set 1 (Using set)
- Dijkstra’s shortest path algorithm using set in STL
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Counting Inversions using Set in C++ STL
- Insertion and Deletion in STL Set C++
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- set::swap() in C++ STL
- set::size() in C++ STL
- set::empty() in C++ STL
- set::erase in C++ STL
- set::emplace() in C++ STL
- set::clear in C++ STL
- Binary Tree to Binary Search Tree Conversion using STL set
- Count smaller elements on right side using Set in C++ STL
- set vs map in C++ STL
- bitset set() function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.