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 7798045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:53:00+00:00 2026-06-01T23:53:00+00:00

I’d like to have a function to remove one element from unordered_set. However, when

  • 0

I’d like to have a function to “remove one element” from unordered_set.

However, when it’s implemented using erase(begin()), it becomes extremely slow.
(This is in g++-4.5.3; maybe begin() has to traverse a larger number of empty hash buckets?)

See the example code below with surprising timings.

Is there some other way to implement “remove one element” that would have greater efficiency? (I do want to allow other intervening set operations which would invalidate iterators.)

#include <unordered_set>
#include <iostream>
#include <chrono>
using namespace std;

struct Timer {
    Timer(string s) : _s(s), _start(Clock::now()) { }
    ~Timer() {
        auto t=chrono::duration_cast<chrono::milliseconds>(Clock::now()-_start).count();
        cerr << "Timer(" << _s << ") = " << t << "ms\n";
    }
 private:
    typedef chrono::high_resolution_clock Clock;
    string _s;
    Clock::time_point _start;
};

int main()
{
    unordered_set<int> s;
    const int num=200000;
    { Timer t("insert"); for (int i=0;i<num;i++) { s.insert(i); } }
    { Timer t("remove half"); for (int i=0;i<num/2;i++) { s.erase(s.begin()); } }
    long long s1=0, s2=0;
    { Timer t("access begin()"); for (int i=0;i<num/2;i++) { s1+=*s.begin(); } }
    { Timer t("access all"); for (auto it=s.begin();it!=s.end();++it) { s2+=*it; } }
    cerr << s1 << " " << s2 << "\n";
    return 0;
}

// Timer(insert) = 36ms
// Timer(remove half) = 3039ms
// Timer(access begin()) = 5958ms
// Timer(access all) = 1ms
  • 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-06-01T23:53:01+00:00Added an answer on June 1, 2026 at 11:53 pm

    It looks like an issue with that version of the GNU library, fixed in more recent versions. Here are the results of my tests, using the two versions I happen to have installed:

    mikes@seymour-desktop:~$ g++-4.4.5 -std=c++0x -O3 test.cpp
    mikes@seymour-desktop:~$ ./a.out 
    Timer(insert) = 15ms
    Timer(remove half) = 3815ms
    Timer(access begin()) = 7722ms
    Timer(access all) = 0ms
    10000000000 14999950000
    
    mikes@seymour-desktop:~$ g++-4.6.1 -std=c++0x -O3 test.cpp
    mikes@seymour-desktop:~$ ./a.out 
    Timer(insert) = 16ms
    Timer(remove half) = 2ms
    Timer(access begin()) = 0ms
    Timer(access all) = 1ms
    10000000000 14999950000
    

    I also got similarly fast results by using boost::unordered_set, so that’s an option if you can’t update your compiler.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm making a simple page using Google Maps API 3. My first. One marker
I would like to count the length of a string with PHP. The string

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.