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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:30:54+00:00 2026-05-27T18:30:54+00:00

I am looping through a vector with a loop such as for(int i =

  • 0

I am looping through a vector with a loop such as for(int i = 0; i < vec.size(); i++). Within this loop, I check a condition on the element at that vector index, and if a certain condition is true, I want to delete that element.

How do I delete a vector element while looping over it without crashing?

  • 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-27T18:30:55+00:00Added an answer on May 27, 2026 at 6:30 pm

    The idiomatic way to remove all elements from an STL container which satisfy a given predicate is to use the remove-erase idiom. The idea is to move the predicate (that’s the function which yields true or false for some element) into a given function, say pred and then:

    static bool pred( const std::string &s ) {
      // ...
    }
    
    std::vector<std::string> v;
    v.erase( std::remove_if( v.begin(), v.end(), pred ), v.end() );
    

    If you insist on using indices, you should not increment the index for every element, but only for those which didn’t get removed:

    std::vector<std::string>::size_type i = 0;
    while ( i < v.size() ) {
        if ( shouldBeRemoved( v[i] ) ) {
            v.erase( v.begin() + i );
        } else {
            ++i;
        }
    }
    

    However, this is not only more code and less idiomatic (read: C++ programmers actually have to look at the code whereas the ‘erase & remove’ idiom immediately gives some idea what’s going on), but also much less efficient because vectors store their elements in one contiguous block of memory, so erasing on positions other than the vector end also moves all the elements after the segment erased to their new positions.

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

Sidebar

Related Questions

When looping through a vector, is it possible to use the index of an
I want to loop through a vector and erase certain elements that correspond to
When looping through a DataRow and encountering types such as DataRow dr; dr[someString] dr[someInteger]
I am looping through a large text file and im looking for lines that
I'm looping through thousands of strings with various regexes to check for simple errors.
I am looping through an array of strings, such as (1/12/1992 apple truck 12/10/10
My program has this function: vector<itemPtr> Level::getItemsAt(const Point& pt) { vector<itemPtr> vect(items.size()); // copy
I'm basically looping through all the entries to check whether some entries is to
While looping through a Dataset which code snippet should I use, should I go
I'm currently looping through a datareader and calling the System.Net.Mail.SmtpClient's Send() method. The problem

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.