std::set#include <set> is a sorted collection of unique elements. The elements are kept in a balanced binary search tree (usually a red-black tree), so inserting, finding and removing an element takes O(log n) time. It is available from the set header:
#include <set>
std::set#include <set><int> s;Elements are added with insert(). Duplicates are ignored:
s.insert(3);
s.insert(1);
s.insert(3); // Ignored.
s.size(); // 2count() returns 1 if the element exists and 0 otherwise:
s.count(1); // 1
s.count(2); // 0find() returns an iterator to the element, or end() if it doesn't exist:
s.find(2) == s.end(); // truecontains() is a clearer way to check whether an element exists:
s.contains(3); // true
s.contains(5); // falseElements are removed by value with erase():
s.erase(1);
s.count(1); // 0Elements can't be modified through an iterator, since changing the element can break the sorted order. Erase the old value and insert the new one instead:
*s.find(3) = 5; // ERROR.
s.erase(3);
s.insert(5);A set can be initialised with a list of values. Duplicates are removed and the elements are sorted:
std::set#include <set><int> s = {3, 1, 2, 1}; // {1, 2, 3}Sets work with range-based for loops and are always iterated in sorted order:
for (const auto& x : s) {
std::cout#include <iostream> << x << "\n"; // 1, 2, 3
}Multiset
std::multiset#include <set> is a sorted collection that allows duplicate elements. It is also available from the set header:
std::multiset#include <set><int> ms;
ms.insert(3);
ms.insert(1);
ms.insert(3);
ms.size(); // 3
ms.count(3); // 2erase() with a value removes every copy. To remove one copy, erase through an iterator:
ms.erase(ms.find(3)); // Removes one 3.
ms.erase(3); // Removes the rest.Unordered Set
std::unordered_set#include <unordered_set> is a collection of unique elements stored in a hash table. Finding, inserting and removing take O(1) time on average, but the elements are in no particular order. It is available from the unordered_set header:
#include <unordered_set>
std::unordered_set#include <unordered_set><int> us = {3, 1, 2, 1}; // Holds 1, 2 and 3 in any order.
us.insert(4);
us.count(1); // 1It has the same insert(), count(), find() and erase() methods as std::set#include <set>.
Note
Use unordered sets when lookups need to be fast and order does not matter. The element type needs a hash function, which built-in types and std::string#include <string> already have through std::hash.
Unordered Multiset
std::unordered_multiset#include <unordered_set> is an unordered collection that allows duplicate elements. It is also available from the unordered_set header:
std::unordered_multiset#include <unordered_set><int> ums = {3, 1, 3};
ums.count(3); // 2Flat Set
std::flat_set#include <flat_set> is a sorted collection of unique elements stored in a sorted std::vector#include <vector> instead of a tree. It is available from the flat_set header:
#include <flat_set>
std::flat_set#include <flat_set><int> fs = {3, 1, 2, 1}; // {1, 2, 3}
fs.insert(0); // {0, 1, 2, 3}
fs.contains(2); // trueFinding an element uses binary search, which takes O(log n) time. Iterating is faster than std::set#include <set> because the elements are contiguous in memory, but inserting and erasing take O(n) time since later elements have to shift.
Note
Use flat sets when elements are rarely inserted or erased but often searched, when elements need to be sorted, or need less memory than std::set#include <set>.
Flat Multiset
std::flat_multiset#include <flat_set> is a sorted, contiguous collection that allows duplicate elements. It is also available from the flat_set header:
std::flat_multiset#include <flat_set><int> fms = {3, 1, 3}; // {1, 3, 3}
fms.count(3); // 2References
- cppreference: std::set
- cppreference: std::multiset
- cppreference: std::unordered_set
- cppreference: std::unordered_multiset
- cppreference: std::flat_set
- cppreference: std::flat_multiset
Dan