News for hackers. Stuff that matters.
Re:In Defense of Linked Lists by asveikau on Friday November 04, 2022 @08:21PM (#33476124)
> [V]ectors outperform other data structures ... even for things that require lookup (i.e. vector outperforming a set)

This is something I've heard before (not often benchmarked) and it makes intuitive sense, and sometimes I've also told it to myself in order to not feel bad for writing O(n) where n is always small.

But somehow, reading this snippet, I'm reminded that the vector does not maintain the semantic reminder that it's used in a key/value use case. So it has me thinking there should be a map/hashtable/dictionary interface that's really just a vector with linear lookup.

Then I think, hmm, maybe the standard containers should just do that. Maybe the authors of those should have some heuristic of "when to get fancy".

Re:In Defense of Linked Lists by KerrAvon on Friday November 04, 2022 @08:34PM (#33476250)
I think the cases where lookup is faster for a linearly scanned vector is where the hash computation overhead is greater than a linear scan. Assuming you have a good hash implementation, that should be true only for a small number of entries, where you need to determine "small" by measuring.
Re:In Defense of Linked Lists by asveikau on Friday November 04, 2022 @08:53PM (#33476418)
I think it's about locality and quickness of compare.

So if you have a small data structure and/or large cache, and the key is not complicated.

Re:In Defense of Linked Lists by bluGill on Friday November 04, 2022 @09:02PM (#33476506)
Even if the key is cheap, that it isn't in cache means that vector will be faster just because we avoid the cache miss of loading the value into memory.
Re:In Defense of Linked Lists by bluGill on Friday November 04, 2022 @09:01PM (#33476495)
Std::find_if works great if you want to treat a vector like a map. Plus you can have different keys if that is useful for your problem (look up employee by name, id, or address?)
Re:In Defense of Linked Lists by dabitude on Saturday November 05, 2022 @02:10AM (#33478462)
These are flat_map, flat_set, flat_hash_map, etc ...

See: https://en.cppreference.com/w/cpp/header/flat_map

Re:In Defense of Linked Lists by usefulcat on Saturday November 05, 2022 @04:31AM (#33479232)
I believe that flat_map and flat_set at least are sorted, so that binary_search/lower_bound can be used for lookups.

Linear search generally does outperform for small collections though (it’s better for caches and the branch predictor).

Re:In Defense of Linked Lists by asveikau on Saturday November 05, 2022 @05:03PM (#33485712)
C++23! The future is here.