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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:06:40+00:00 2026-05-28T04:06:40+00:00

I have some code that adds to a std::vector and a std::map after creating

  • 0

I have some code that adds to a std::vector and a std::map after creating an object.

v.push_back(object);     // std::vector
m[object->id] = object;  // std::map

I want to make this have a strong exception guarantee. Normally, to make operations like these atomic, I would implement a swap method for each container, and call all of the functions that could throw on temporary copies of the container:

vector temp_v(v);
map temp_map(m);

temp_v.push_back(object);
temp_m[object->id] = object;

// The swap operations are no-throw
swap(temp_v, v)
swap(temp_m, m)

However, making temporary copies of the entire vector and map seems very expensive. Is there any way to implement a strong exception guarantee for this function without the expensive copies?

  • 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-28T04:06:41+00:00Added an answer on May 28, 2026 at 4:06 am

    General Case

    Technically, only one copy is required:

    1. Copy the vector
    2. Update the copy
    3. Update the map
    4. Swap the copy and the original vector

    Another option is catch-roll-back-and-rethrow:

      v.push_back(object);
      try
      {
        m.insert(object->id, object); // Assuming it cannot be present yet
      }
      catch(..)
      {
        v.pop_back();
        throw;
      }
    

    Or the other way around. I picked this order because the vector::pop_back() is guaranteed not to fail.

    UPDATE: If object->id could be present, see Grizzly’s answer for a solution.


    Specific Case for Pointers

    However, as you are using object->, you might be storing pointers. The copy-constructor of a pointer cannot throw, and we can use that fact to simplify the code:

    v.reserve(v.size() + 1);
    m[object->id] = object; // can throw, but not during the assignment
    v.push_back(object); // will not throw: capacity is available, copy constructor does not throw
    

    And if you are really worried about frequent resizing:

    if (v.capacity() == v.size()) v.resize(v.size()*2); // or any other growth strategy
    m[object->id] = object;
    v.push_back(object);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that adds properties to an object like this: var MyObject
I have some setup code in my functions.php file that sets permalinks and adds
I have some code that effectively does this : File file = new File(C:\\Program
I have some code that adds mouseover events and mouseout events to all 'a'
CODE I have some code that adds a UILongPressGestureRecognizer gesture recognizer called _recognizer to
I have some code that adds a flag to an email but when I
I have some code below that creates a RelativeLayout and adds a button to
I have some code that outputs like this: void exec_prompt(char * usr_name, char *
I have some code that gives a user id to a utility that then
I have some code that uses the shared gateway pattern to implement an inversion

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.