Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use std::set or std::unordered_set for a set of pointers?

I have a set of pointers. In the first step, I insert data pointers, and in the second step, I iterate over the whole set and do something with the elements. The order is not important, I just need to avoid duplicates, which works fine with pointer comparison.

My question is, whether it might be advantageous to use an unordered set for the same purpose. Is insertion faster for an unordered set?

like image 738
Fabian Avatar asked Feb 19 '16 12:02

Fabian


People also ask

Is unordered_set faster than set?

For a small number of elements, lookups in a set might be faster than lookups in an unordered_set . Even though many operations are faster in the average case for unordered_set , they are often guaranteed to have better worst case complexities for set (for example insert ).

Should I use set or unordered set?

Use unordered_set whenWe need to keep a set of distinct elements and no ordering is required. We need single element access i.e. no traversal.

What is the difference between unordered_set and set?

Set is an ordered sequence of unique keys whereas unordered_set is a set in which key can be stored in any order, so unordered. Set is implemented as a balanced tree structure that is why it is possible to maintain order between the elements (by specific tree traversal).

Is unordered set faster than set C++?

Even though unordered set is faster in the average case, set is almost always guaranteed to have lower worst-case complexities (for example insert). If you want to access the elements in order, that set sorts them. Different sets can be lexicographically compared using <,<=, >, and >=.


1 Answers

As Ami Tavory commented, if you don't need order, then it's usually best to go for unordered containers. The reason being that if order somehow improved performance, unordered containers would still be free to use it, and hence get the same or better complexity anyhow.

A downside of unordered collections is that they usually require a hash function for the key type. If it's too hard or expensive to make one, then containers which don't use hashes might be better.

In C++'s standard library, the average insertion complexity for std::set is O(log(N)), whereas for std::unordered_set it's O(1). Aside from that, there are probably less cache misses on average when using std::unordered_set.

At the end of the day though, this is just theory. You should try something that sounds good enough and profile it to see if it really is.

like image 114
Yam Marcovic Avatar answered Oct 06 '22 00:10

Yam Marcovic