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?
clear()
erases all elements in the container. – Soongstd::vector::reserve
:bucket_count
is part of the observable state; it is allowed to be changed on insertion, but it's not explicitly allowed to change onrehash
orreserve
(or even onerase
as far as I can see..). – Smallpoxbucket_count
... not explicitly allowed to change onrehash
orreserve
" - the latter exist to allow the number of bucket (thusbucket_count()
) to be modified - the former directly accepts a new number of buckets (but is subject to asize() / max_load_factor()
sanity check) and the latter derives it from a number of anticipated elements and the currentmax_load_factor
. "explicit" or not that's what they do. – Scrogginsrehash
andreserve
, but notclear
norerase
. – Smallpox