Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does clear() affect the bucket count of std::unordered_set?

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?

like image 911
Jonathan H Avatar asked Sep 04 '14 23:09

Jonathan H


1 Answers

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.

like image 152
Anton Savin Avatar answered Sep 30 '22 19:09

Anton Savin