C++ Set
A std::set stores unique elements in sorted order. Adding a value that is already present does nothing, and iterating always visits the elements from smallest to largest. Sets are perfect for questions like "have I seen this before?" and "what are the distinct values?". Include <set>.
Creating and filling a set
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
set<int> numbers = {7, 3, 9, 3, 1, 7};
for (int n : numbers) cout << n << " ";
cout << "(size " << numbers.size() << ")\n";
set<string> tags;
tags.insert("cpp");
tags.insert("algorithms");
tags.insert("cpp"); // duplicate: ignored
tags.insert("beginner");
for (const auto& t : tags) cout << t << " ";
cout << "\n";
}1 3 7 9 (size 4)
algorithms beginner cpp
Duplicates disappeared and the elements came out sorted — strings in alphabetical order.
Searching and removing
A set is implemented as a balanced binary search tree, so lookups, insertions and removals all take O(log n) time — fast even for millions of elements.
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> s = {10, 20, 30, 40, 50};
cout << boolalpha;
cout << "contains 30: " << s.contains(30) << "\n"; // C++20
cout << "count(35): " << s.count(35) << "\n"; // 0 or 1
auto [it, inserted] = s.insert(20);
cout << "inserted 20 again? " << inserted << "\n";
s.erase(40);
if (auto pos = s.find(50); pos != s.end()) {
cout << "found " << *pos << "\n";
}
// lower_bound: first element >= value
cout << "first >= 25: " << *s.lower_bound(25) << "\n";
for (int x : s) cout << x << " ";
cout << "\n";
}contains 30: true
count(35): 0
inserted 20 again? false
found 50
first >= 25: 30
10 20 30 50
insert returns a pair: an iterator to the element and a bool telling you whether it was actually added.
Note: Elements of a set are read-only. You cannot change a value in place, because that could break the sorted order — erase it and insert the new value instead.
Example: unique words
#include <iostream>
#include <set>
#include <sstream>
#include <string>
using namespace std;
int main() {
string text = "the cat saw the dog and the dog saw the cat";
istringstream in(text);
set<string> unique;
string word;
int total = 0;
while (in >> word) {
unique.insert(word);
total++;
}
cout << total << " words, " << unique.size() << " unique:\n";
for (const auto& w : unique) cout << " " << w << "\n";
}11 words, 5 unique:
and
cat
dog
saw
the
Custom order and set operations
Pass a comparator as the second template argument to change the order. The <algorithm> header provides set operations on sorted ranges:
#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
#include <functional>
using namespace std;
int main() {
set<int, greater<int>> desc = {4, 1, 8, 2};
for (int x : desc) cout << x << " ";
cout << "\n";
set<int> a = {1, 2, 3, 4, 5};
set<int> b = {4, 5, 6, 7};
set<int> both, either, onlyA;
set_intersection(a.begin(), a.end(), b.begin(), b.end(), inserter(both, both.begin()));
set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(either, either.begin()));
set_difference(a.begin(), a.end(), b.begin(), b.end(), inserter(onlyA, onlyA.begin()));
auto show = [](const char* label, const set<int>& s) {
cout << label;
for (int x : s) cout << x << " ";
cout << "\n";
};
show("intersection: ", both);
show("union: ", either);
show("difference: ", onlyA);
}8 4 2 1
intersection: 4 5
union: 1 2 3 4 5 6 7
difference: 1 2 3
Related containers
| Container | Header | Ordered? | Duplicates? | Lookup |
|---|---|---|---|---|
std::set |
<set> |
yes | no | O(log n) |
std::multiset |
<set> |
yes | yes | O(log n) |
std::unordered_set |
<unordered_set> |
no | no | O(1) average (hash table) |
Tip: If you never need sorted order,
unordered_setis usually faster. Its iteration order is unspecified, though, so don't rely on it when printing.
Test yourself
What does iterating over set<int>{5, 1, 5, 3} print?
- 5 1 5 3
- 5 1 3
- 1 3 5
- 1 3 5 5
What is the time complexity of find in a std::set?
- O(1)
- O(log n)
- O(n)
- O(n log n)
Which container allows duplicate values but keeps them sorted?
- set
- unordered_set
- multiset
- deque