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, 2022 in C++ by Nicholas
• 7,760 points
2,817 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, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Why use #define instead of a variable

What is the purpose of the #define ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
345 views
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, 2022 in C++ by Nicholas
• 7,760 points
506 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 Jun 27, 2022 in C++ by Damon
• 4,960 points
420 views
0 votes
0 answers

When and why do I need to use cin.ignore() in C++?

In C++, I developed a simple application that requested the user to enter a number and then a string.  Surprisingly, when I ran the application, it never paused to ask for the string.  It simply ignored it.  After conducting some research on StackOverflow, I discovered that I needed to include the following line: cin.ignore(256, '\n'); before the line with the string input  That addressed the problem and allowed the software to run.  My issue is why C++ need the cin.ignore() line, and how can I forecast when I will need to use it. Here's the software I created: #include <iostream> #include <string> using namespace std; int main() { ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
709 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, 2022 in Python by Rahul
• 9,680 points
1,698 views
0 votes
1 answer

Competitive Programming Algorithm Sock Drawing Probability Question

Another way to look at the problem ...READ MORE

answered Mar 25, 2022 in Machine Learning by Dev
• 6,000 points
563 views
0 votes
0 answers

Is hiding implementation detail Encapsulation or Abstraction?

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

May 6, 2022 in Java by narikkadan
• 63,720 points
2,945 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, 2022 in C++ by Damon
• 4,960 points
10,896 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, 2022 in C++ by Damon
• 4,960 points
415 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP