std::set is ordered, associative container. This means that the type you store in the set must have a way to order themselves,
Or to put it another way, an operator<. Sets hold unique items, duplicate elements will be ignored. Sets use the < operator to determine duplication:
if !(x < y> && !(y < x) {
Because elements are ordered, it is very fast to find elements in a set.
#include <iostream>
#include <set>
int main() {
std::set<int> ints {10, 2, 9, 4, 5, 6, 7, 2, 3, 5, 8, 3};
for (int i: ints) {
std::cout << i << std::endl;
}
return 0;
}
Some common methods of the std::set type are insert, which inserts a value into the set. Insert returns a pair which holds the state of the insert. In the first position, the data inserted and in the second position, a boolean indicating success., Emplace, which does the same as insert but builds the object in place, size, which returns how many items are in the set. We can loop a set using both a standard and enhanced for loop. Set supports both forward and reverse iterators.