set vs unordered_set in C++ STL Last Updated : 10 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Differences : | 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. Examples: 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. Implementation: CPP // Program to print elements of set #include <bits/stdc++.h> using namespace std; int main() { set<int> s; s.insert(5); s.insert(1); s.insert(6); s.insert(3); s.insert(7); s.insert(2); cout << "Elements of set in sorted order: \n"; for (auto it : s) cout << it << " "; return 0; } OutputElements of set in sorted order: 1 2 3 5 6 7 Implementation: CPP // Program to print elements of set #include <bits/stdc++.h> using namespace std; int main() { unordered_set<int> s; s.insert(5); s.insert(1); s.insert(6); s.insert(3); s.insert(7); s.insert(2); cout << "Elements of unordered_set: \n"; for (auto it : s) cout << it << " "; return 0; } OutputElements of unordered_set: 2 7 3 6 5 1 Predecessor/Successor in Set: Set can be modified to find predecessor or successor whereas Unordered_set doesn't allow to find predecessor/Successor. Implementation: CPP // Program to print inorder predecessor and inorder successor #include <bits/stdc++.h> using namespace std; set<int> s; void inorderPredecessor(int key) { if (s.find(key) == s.end()) { cout << "Key doesn't exist\n"; return; } set<int>::iterator it; it = s.find(key); // get iterator of key // If iterator is at first position // Then, it doesn't have predecessor if (it == s.begin()) { cout << "No predecessor\n"; return; } --it; // get previous element cout << "predecessor of " << key << " is="; cout << *(it) << "\n"; } void inorderSuccessor(int key) { if (s.find(key) == s.end()) { cout << "Key doesn't exist\n"; return; } set<int>::iterator it; it = s.find(key); // get iterator of key ++it; // get next element // Iterator points to NULL (Element does // not exist) if (it == s.end()) { cout << "No successor\n"; return; } cout << "successor of " << key << " is="; cout << *(it) << "\n"; } int main() { s.insert(1); s.insert(5); s.insert(2); s.insert(9); s.insert(8); inorderPredecessor(5); inorderPredecessor(1); inorderPredecessor(8); inorderSuccessor(5); inorderSuccessor(2); inorderSuccessor(9); return 0; } Outputpredecessor of 5 is=2 No predecessor predecessor of 8 is=5 successor of 5 is=8 successor of 2 is=5 No successor Let us see the differences in a tabular form -: setunordered_set1.It is used to store the unique elements.It is used to store the unique elements.2.Sets are implemented using Binary search trees.It is implemented using hash table3.It stores the elements in increasing order.It stores the element with no order.4.We can traverse sets using iterators.We can traverse unordered_set using iterators.5.It is included in #include <set> header file.It is included in #include <unordered_set> header file. Comment More infoAdvertise with us A Abhishek rajput Follow Improve Article Tags : Hash Binary Search Tree Competitive Programming Difference Between C++ DSA STL cpp-unordered_set cpp-set +5 More Practice Tags : CPPBinary Search TreeHashSTL Similar Reads Set in C++ STL In C++, sets are associative container which stores unique elements in some sorted order. By default, it is sorted ascending order of the keys, but this can be changed as per requirement. It provides fast insertion, deletion and search operations.Example: C++#include <iostream> #include <se 7 min read Different Ways to Initialize an Set in C++ Initializing a set means assigning some initial values to the elements of the set container. In this article, we will learn different methods to initialize an std::set in C++.Table of ContentUsing Initializer ListOne by One InitializationFrom Another std::setFrom Another STL Container or ArrayUsing 3 min read C++ STL Set Insertion and Deletion Prerequisite: Set A Set is a container implemented in C++ language in STL and has a concept similar to how the set is defined in mathematics. The fact that separates the set from the other containers is that it contains only the distinct elements and elements can be traversed in sorted order. Having 7 min read Different Ways to Insert Elements in Set in C++ STL Prerequisites: Set in C++ The C++ Standard Template Library offers containers called Sets. It functions essentially in the same ways as a binary search tree and is used to store different elements in increasing/decreasing order. There are different methods to insert elements in the set as mentioned 3 min read How to Access Elements in Set by Index in C++? In C++, elements of a set cannot be accessed directly by index or position. However, we can work around this limitation using iterators. In this article, we will learn how to access the elements in set by index in C++.The most efficient way to access a set element by index is to use the std::next() 3 min read Different ways to iterate over a set in C++ Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific order. Syntax: set<datatype> setname; Here,Datatype: Set can take any data type depending on the values, e.g. int, char, float, et 5 min read Commonly Used Methodsset::begin() and set::end() in C++ STLIn C++, std::set::begin() and std::set::end() are built-in functions used to retrieve set::iterators to the beginning and the end of the set container. Set uses bidirectional iterators, so the iterators returned by these functions support the dereferencing, increment, decrement, relational, and equa 3 min read set::size() in C++ STLIn C++, set::size() function is a built-in used to find the number of elements in the given set container. It is the member function of std::set class defined inside <set> header file. In this article, we will learn about the std::set::size() method in C++.Example:C++// C++ Program to illustra 2 min read set::empty() in C++ STLSets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::empty() empty() 2 min read set::insert() function in C++ STLThe std::set::insert() is a built-in function of C++ STL set container which is used to insert new elements in it. In this article, we will learn how to use set::insert() function in our C++ programs.SyntaxThe string::replace() function provides 6 different overloads for different purposes:st.insert 4 min read set::emplace() in C++ STLSets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::emplace() This f 4 min read set find() Function in C++ STLThe std::set::find() is a built-in function in C++ STL that is used to find an element in the set container. It is a member function of std::set container so we can use it directly with any set object.Syntax set_name.find(key) Parameterskey: The element which we have to find.Return ValueIf the eleme 2 min read set::count() Function in C++ STLThe std::set::count() is a built-in function in C++ STL which is used to count the number of times an element occurs in the set container. std::set container stores unique elements, so it can only return 1 or 0. Therefore, it is only used for checking if the element exists in the set or not.ExampleC 3 min read set::erase in C++ STLIn C++, the std::set::erase() is a built-in member function of std::set container that is used to remove the element(s) from the container. In this article, we will learn how we can use set::erase() function in our C++ program.The set::erase() can be used in different ways to erase element(s) from t 3 min read set::clear in C++ STLSets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::clear() clear() 2 min read set::swap() in C++ STLSets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::swap() This func 2 min read Other Member Methodsset max_size() function in C++ STLThe set::max_size() is a built-in function in C++ STL which returns the maximum number of elements a set container can hold. Syntax: set_name.max_size() Parameters: This function does not accept any parameters. Return Value: This function returns the maximum number of elements a set container can ho 1 min read set emplace_hint() function in C++ STLThe set::emplace_hint() is a built-in function in C++ STL which inserts a new element in the set. A position is passed in the parameter of the function which acts as a hint from where the searching operation starts before inserting the element at its current position. The position only helps the pro 2 min read set::rbegin() and set::rend() in C++ STLset::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container. Syntax: reverse_iterator set_name.rbegin() Parameters: The function does not take any parameter. Return value: The function returns a reverse iterator pointing to the last 2 min read set crbegin() and crend() function in C++ STLThe set::crbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the last element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant 2 min read set cbegin() and cend() function in C++ STLThe set::cbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the first element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant 2 min read set::key_comp() in C++ STLset::key_comp() is an inbuilt function in C++ STL which returns a copy of the comparison object used by the container. By default, this is a less object, which returns the same as operator '. This object determines the order of the elements in the container. It is a function pointer or a function ob 2 min read set::lower_bound() Function in C++ STLThe std::set::lower_bound() method is used to find the first element in the set that is equal to or greater than the given value. It is a member function of std::set class and is defined inside <set> header file. In this article, we will learn about std::set::lower_bound() function in C++.Exam 3 min read Set upper_bound() in C++ STLIn C++, the set upper_bound() is a built-in method used to find the first element in the set that is just greater than the given value. In this article, we will learn about set upper_bound() function in C++.Letâs take a quick look at a simple illustration of the function:C++#include <bits/stdc++. 3 min read set equal_range() function in C++ STLThe set::equal_range() is a built-in function in C++ STL which returns an iterator of pairs. The pair refers to the range that includes all the elements in the container which have a key equivalent to k. Since set contains unique elements, the lower bound will be the element itself and the upper bou 3 min read set operator= in C++ STLThe â=â is an operator in C++ STL which copies (or moves) a set to another set and set::operator= is the corresponding operator function. There are three versions of this function: The first version takes reference of an set as an argument and copies it to an set. Syntax: ums1.operator=(set &set 2 min read set get_allocator() in C++ STLThe set::get_allocator() in C++ STL is an in-built function which returns the copy of the allocator object associated with the set. Syntax: mulset.get_allocator(); Parameters: This function does not accept any parameters. Return Value: This function returns the allocator associated with the set. Tim 2 min read Difference between std::set::upper_bound and std::upper_bound in C++ Prerequisites: Random-access Iterators, Bidirectional Iterators Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and 4 min read Difference between std::set vs std::vector in C++ STL Vectors: Vectors are containers similar to dynamic arrays, with the ability to resize when a new element is inserted or deleted from it. It is a template of Standard Template Library or STL, which provides more flexibility to the program. Elements of vectors are placed in contiguous storage and are 2 min read Like