Why would anyone use set instead of unordered set

0 votes
C++0x introduces unordered set, which can be found in boost and many other places.

According to my understanding, unordered set is a hash table with an O(1) lookup complexity.

Set, on the other hand, is nothing more than a tree with log(n) lookup complexity.

Why would anyone use set rather than unordered set?

Is there still a need for set?
May 31 in C++ by Nicholas
• 2,460 points
34 views

1 answer to this question.

0 votes
Unordered sets must compensate for their O(1) average access time in several ways:

To store the same number of elements, set uses less memory than unordered set.

Lookups in a set may be faster than lookups in an unordered set for a small number of elements.

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 >=.

These operations do not require unordered sets to be supported.
answered Jun 1 by Damon
• 3,780 points

Related Questions In C++

0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2 in C++ by Nicholas
• 2,460 points
21 views
0 votes
1 answer

Use of multisets in C++ [duplicate]

Because a multi-set does not need the storage of single-element objects.  You're considering storing anything in a multi-set, such as a string.  But it is not its intended use.  You may use whatever struct you want and compare it to a single element in the struct. As an example: struct PhoneBookEntry { std::string name; ...READ MORE

answered 2 days ago in C++ by Damon
• 3,780 points
13 views
0 votes
1 answer

why are copy constructors needed and what are the cases where they are very helpful?

A copy constructor is a member function ...READ MORE

answered May 31 in C++ by Damon
• 3,780 points
34 views
0 votes
1 answer

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

answered May 31 in C++ by Damon
• 3,780 points
11 views
0 votes
1 answer

What is the difference between tree depth and height?

To answer your question, you will have ...READ MORE

answered Feb 9 in Python by Rahul
• 8,980 points
29 views
0 votes
1 answer
0 votes
0 answers

Is hiding implementation detail Encapsulation or Abstraction?

I have seen some people defining abstraction ...READ MORE

May 6 in Java by narikkadan
• 10,840 points
55 views
0 votes
0 answers
0 votes
1 answer

Use of min and max functions in C++

The functions fmin and fmax are designed ...READ MORE

answered Jun 21 in C++ by Damon
• 3,780 points
15 views
0 votes
1 answer

Use of "this" keyword in C++ [duplicate]

Yes, it is optional and generally omitted.  However, it may be essential for accessing variables after they have been overridden in the scope: Person::Person() { int age; ...READ MORE

answered Jun 20 in C++ by Damon
• 3,780 points
14 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP