Which of those two is faster for random insertions and deletions?
My guess is list.
Though having the values as the keys as in the case of sets is attractive too.
Is performance similar for iterating over the whole container?
Set is an ordered sequence of unique keys whereas unordered_set is a set in which key can be stored in any order, so unordered. Set is implemented as a balanced tree structure that is why it is possible to maintain order between the elements (by specific tree traversal).
Vector is faster for insertion and deletion of elements at the end of the container. Set is faster for insertion and deletion of elements at the middle of the container.
Sets are the containers in C++ STL for storing elements in a given order. The elements of a set must be unique.
List
Set
std::list is O(1) for inserts and deletions. But you may well need O(n) to find the insertion or deletion point. std::set is O(log(n)) for inserts and deletions, it is usually implemented as a red-black tree.
Consider the effort to find the insert/delete point to make your choice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With