std::set is an associative container that stores unique elements in strictly sorted order. std::multiset allows duplicate elements while preserving sorting.
lower_bound & upper_bound)#include <iostream>
#include <set>
int main() {
std::set<int> scores = {45, 88, 12, 95, 60};
// Automatic ascending sorted order: 12, 45, 60, 88, 95
for (int s : scores) {
std::cout << s << " ";
}
std::cout << "\n";
// Fast logarithmic range search: Find first score >= 60
auto it = scores.lower_bound(60);
if (it != scores.end()) {
std::cout << "First qualifying score >= 60: " << *it << "\n"; // 60
}
return 0;
}
Boost your C++ proficiency by studying these closely connected core modules:
Associative key-value pairs backed by Red-Black trees.
Hash table O(1) sets vs Red-Black tree O(log N) sets.
Iterating through sorted trees.