There are plenty of answers with std::vector
, but what about std::unordered_set
?
My real question (closely related) is this; is it efficient to reuse the same unordered set by clearing it before each use, if I reserve what I know to be a reasonable size beforehand?
Formal answer is: it depends on implementation.
Informal answer is: unordered_set
inside has an array (of some kind) of buckets, and most likely the implementation is consistent with vector
, so this array won't be deleted when clear()
is called. So calling clear()
most likely will give some benefit.