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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:27:37+00:00 2026-06-12T03:27:37+00:00

Suppose you have an std::unordered_set<std::shared_ptr<A>> as; // (there is an std::hash<std::shared_ptr<A>> specialisation) and you

  • 0

Suppose you have an

std::unordered_set<std::shared_ptr<A>> as;
// (there is an std::hash<std::shared_ptr<A>> specialisation)

and you want to replace some of its elements while iterating over it:

for (auto it = as.begin(); it != as.end(); ++it) {
  if ((*it)->condition()) {
    as.erase(it);
    as.insert(std::make_shared<A>(**it));
  }
}

This could invalidate the iterator at erase and insert (if rehashing takes place), so this loop will exhibit undefined behaviour and will most likely crash horribly.

The one solution I can think of is using two separate vectors to buffer the insert and erase operations and later use the overloads that take iterator pairs for erasing and inserting (this is presumably more rehashing-friendly).

Even if I use the buffer approach, this still seems bloated code and could result in two rehashes that might possibly both be unnecessary.

So, is there a better way to do 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-06-12T03:27:38+00:00Added an answer on June 12, 2026 at 3:27 am

    I just thought of a possible approach (just after asking) but maybe there are even better ones.

    Copying everything to a vector and then rebuilding the set from the vector should be faster:

    std::vector<std::shared_ptr> buffer;
    buffer.reserve(as.size());
    for (auto it = as.begin(); it != as.end(); ++it) {
      if ((*it)->condition()) {
        buffer.push_back(std::make_shared<A>(**it));
      } else {
        buffer.push_back(*it);
      }
    }
    as = std::unordered_set<std::shared_ptr<A>>(buffer.begin(),buffer.end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this function: std::string Func1(std::string myString) { //do some string processing std::string
For example, suppose I have std::string containing UNIX-style path to some file: string path(/first/second/blah/myfile);
Suppose I have two containers of elements: std::vector<std::string> foo = { aa, bb, cc
Suppose I have some template class forward declared and I want to typedef a
Suppose I have a vector std::vector<a> A; I can get access to its member-functions
Suppose I have something like this: class Collection { private: typedef std::vector< std::shared_ptr<Something> >::iterator
Suppose I have the following: struct Person { std::string mName; Birthday mBirthday; }; using
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
Suppose I have a vector of ints, std::vector<int> numbers; that is populated with a
Suppose I have a class like this: #include <iostream> using namespace std; class Boda

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.