Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6646951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:28:46+00:00 2026-05-26T00:28:46+00:00

I have an unordered_set as follow: unordered_set <long> valueSet; /*the following insertion is done

  • 0

I have an unordered_set as follow:

unordered_set <long> valueSet;

/*the following insertion is done in order (from 1 to 10000), 
 *unordered_set will keep the elements based on the insertion order, right, 
 *just like in a vector ?
**/

for(long i = 1; i <= 10000;++i)
{
        valueSet->insert(i);
}

Then I performed another function which erased about 85% of the elements in that unordered_set. (The elements which are to be erased depend on the logic of this function, but it doesn’t matter since all the elements were initially inserted in order).

Now after erasing some of the elements in the unordered_set, I want to print the last element which still remains in that unordered_set. For instance, element 9997, 9998, 9999, and 10000 have been erased, so the largest remaining element in this set is 9996.
How to do this?
If using a basic set, I can do the following:

set <long>::reverse_iterator it = valueSet.rbegin();
cout << *it << endl;

In a set, we have the reverse_iterator and also rbegin(), but this does not exist in unordered_set. The reason why I did not the basic set is that I need the element size to scale up to 10^8. Using the regular set (which is based on red black trees) will kill the performance indeed (especially when it deals to insertion and deletion).
How can I do this? Copying the final remaining unordered_set to a vector will work, but of course this will take time. How can I achieve this by using a smarter way? I notice that I also cannot do something like :

unordered_set <long>::iterator it = valueSet.end();
//operator -- does not exist here in the unordered_set
it--;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:28:47+00:00Added an answer on May 26, 2026 at 12:28 am

    From what I’ve gathered from your comments, using a std::bitset or its dynamic counterpart boost::dynamic_bitset should be appropiate here. You get O(1) insertion and deletion and O(N) for determining the maximum element (by linear search). One could even argue that finding the maximum is amortized O(1), as you have to do at most as many search steps as deletion operations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to have a function to remove one element from unordered_set. However, when
I have an unordered tree. Each node represents a task that can be done
I have an unordered list with a few list elements. #tags { width: 300px;
I currently have a drop-line horizontal menu using an unordered list with the following
Suppose I have an unordered set unordered_set<int> my_set; myset.insert(1); myset.insert(2); myset.insert(3); How do I
I have a program that loads a file (anywhere from 10MB to 5GB) a
I have a set of <li> elements in an unordered list. The items are
I have a folder content type that derives from ATBTreeFolder which in plone 4(actually
I have to rewrite windows-code into crossplatform view. Here is the example: std::unordered_set<Type>::iterator it
I have an stl unordered_map and I would like to store references to elements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.