r/cpp • u/BgA_stan • May 03 '26
When to actually use a set
https://dubeykartikay.com/posts/why-never-use-std-unordered-set/reposted with a different title, as i got feedback the previous title was clickbait
21
Upvotes
33
u/SnooStories6404 May 03 '26
The article title is "Why You Should Never Use a set" but goes on to say sometimes you should use a set.
14
u/mcmcc #pragma once May 03 '26
For integer-like keys and sizes <1000, I prefer std::vector sorted by judicious use of
lower_bounds().Almost always fast enough (if not fastest) and visualizes well in the debugger.
12
u/SubjectiveMouse May 03 '26
Would be helpful to include flat_set, where many shortcomings of unordered_set are not present. It may show a completely different picture
13
u/johannes1971 May 03 '26
I don't think the comparison is fair: in the bitset example, you leverage extra knowledge that isn't available to the set versions. Good on you for doing that, but that means you aren't making a general statement about sets vs. direct lookups. At best it's a statement that you can _sometimes_ use additional knowledge to get additional performance gains.
Or in other words, if your identifiers had not been limited-range integers, but UUIDs, this would not have worked.
9
u/MiddleSky5296 May 03 '26
C’mon you don’t have to make things more complicated by using an array instead of a set for set use cases (even if the inserted elements are dense). unordered_set, set, and bitset are efficient enough for most of the deduplication use cases. Save your cognitive load for actual business logic.
3
u/vI--_--Iv May 03 '26
Isn't it common knowledge?
4
u/DevaBol May 03 '26
Bloomberg BDE uses this techniques for calendars: they just use an array of bits (business or holiday) for each date in the range of interest
1
u/Numerous-Door-6646 May 06 '26
I creates a set of benchmarks for this comparing std::unordered_set to boost dynamic bitset, and then against various other implementations: (sort-unique, ranges-sort-unique, std::bitset (heap and stack), plf::bitset (heap and stack)). https://github.com/paperclip/set_perf
It is impressive how well sort_unique does.
3
u/alex-weej May 03 '26
For years I've mused about having a `std::do_the_right_thing_set<$SOME_UUID>` (or map) that collects profile data and allows it to select a reasonable implementation based on that profile. All it takes is a change to one line of code to change the cost-benefit.
3
u/csb06 May 04 '26
Stepanov had similar advice regarding the usage of std::set:
source (course notes from a class he taught)
3
u/R3DKn16h7 May 03 '26
I disagree. If a mathematical set with unknown elements is actually what you are going for as a data structure, then set makes sense. There is no point in prematurely optimizing something to the detriment of clearly showing intent with code and data structures you use.
Only AFTER you benchmark and prove there is significant gain in using another data structure, should you use something different.
1
u/cristi1990an ++ May 05 '26
Hopefully it's common knowledge at this point that if you care about performance you should avoid using the associative containers provided by the standard library of any vendor.
1
u/bolzan0 May 08 '26
Aby byl