C++ Data StructuresLesson 53Free preview
C++ Sets
This lesson is part of C++ · All levels
Summary
Std::set is a container that only stores unique elements and always sort them automatically based on values, even though our order of insertion is random. Because the structure is a balanced binary tree, the average search, insertion, and deletion takes logarithmic time. Sets are suitable for data that cannot be duplicated, such as a list of IDs, unique words, or a set of measurement results.
Key points
- Set rejects duplicate: the addition of an existing value is ignored.
- Elements are always ordered by the operator < (can be changed with a comparator).
- Find(k) returns the iterator to the k position or end() when it does not exist.
- Insert, erase, and find work in time.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.