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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:11:15+00:00 2026-05-15T18:11:15+00:00

I have an STL container and I need to perform an action on each

  • 0

I have an STL container and I need to perform an action on each element in the container. But if the action fails on any element, I want to reverse the action on any elements that have already been changed.

For example, if I had an STL vector with pointers to a number bankAccount classes and wanted to increase each one by $50. But if any of the bank accounts fail to increase by 50, I want to cancel the increase entirely and decrease by $50 any of the accounts that have already been increased.

std::vector<bankAccount*> bankAccounts;
std::vector<bankAccount*>::iterator iter;

for (iter = bankAccounts.begin(); iter != bankAccounts.end(); ++iter)
{
    try
    {
        iter->increaseBalance(50);
    }
    catch (...)
    {
        // One of the bankAccounts failed to increase by 50, now I need to go 
        // back and decrease by 50 all of the bankAccounts that have already 
        // been increased.
    }
}

Is there any elegant way to do this? Maybe with STL algorithms or using reverse iterators?

  • 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-15T18:11:16+00:00Added an answer on May 15, 2026 at 6:11 pm

    Here’s what I would do:

    • Move the try/catch outside the loop
    • Create a duplicate of the bankAccounts container
    • Iterate over the duplicate container, calling increaseBalance on each item
    • If the loop sucessfully completed, swap() the original and the duplicate container

    The code would look something like this:

    std::vector<bankAccount> bankAccounts;
    ...
    std::vector<bankAccount> tmp(bankAccounts);
    
    try
    {
       for (iter = tmp.begin(); iter != tmp.end(); ++iter)
       {
         iter->increaseBalance(50);
       }
       bankAccounts.swap(tmp);
    }
    catch (...)
    {
    }
    

    Please note that holding a pointer to an object inside a std::vector is generally not that good an idea as the container expects the data stored in it to have value semantics, not pointer semantics. This can lead to dangling pointers, memory leaks and also requires additional cleanup code that you don’t need otherwise (to delete the items in container manually). With the code above, I’ve switched to holding the data inside the vector, if that’s not an option you need to ensure that you’re using a manual deep copy when you’re copying the vector.

    Actually, you can reduce the code to the following if you assume the same definitions for bankAccounts and tmp:

    std::for_each(tmp.begin(), tmp.end(),
                  std::mem_fun_ref(&bankAccount::increaseBalance, 50));
    bankAccounts.swap(tmp);
    

    The main advantage of the code above is that in both cases, it is exception safe without any further special handling.

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

Sidebar

Related Questions

I have two STL containers that I want to merge, removing any elements that
I'm looking for a STL container that works like std::multimap, but has constant access
I have a join function that operates on STL strings. I want to be
Which STL container would fit my needs best? I basically have a 10 elements
I have a container class (called Atom) that I want to store objects of
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead
I have a class TContainer that is an aggregate of several stl collections pointers
I have a stl::list containing Widget class objects. They need to be sorted according
If i have a stl map from string to int and i want to
Suppose I have a STL map where the values are pointers, and I want

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.