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

  • Home
  • SEARCH
  • 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 9009673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:15:51+00:00 2026-06-16T02:15:51+00:00

I want to process elements from a vector for some time. To optimize this

  • 0

I want to process elements from a vector for some time. To optimize this I don’t want to remove an item when it is processed and I remove all processed items at the end.

vector<Item*>::iterator it;
for(it = items.begin(); it != items.end(); ++it)
{
    DoSomething(*it);

    if(TimeIsUp())
    {
        break;
    }
}

items.erase(items.begin(), it);

Is it safe to use erase when it == items.end()? In documentaion it is said that erase() will erase [first,last) and this should be safe but I want to be sure.

EDIT:

Is it safe to use std::vector.erase(begin(), begin())?

  • 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-16T02:15:52+00:00Added an answer on June 16, 2026 at 2:15 am

    What that [first,last) means is everything between first and last, including first but not including last. It denotes a half-open set. Had they used [first,last], last would also be included; had they they used (first,last), first and last would be excluded.

    There is a problem with your code. You want the element at it to be deleted if it isn’t equal to end(). Instead of items.erase (items.begin(),it), you should be using

    if (it != items.end()) {
       items.erase (items.begin(), it+1);
    }
    else {
       items.clear(); // same as items.erase (items.begin(), items.end())
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I remove the ith item from a std::vector ? I know I
I want to process every element of a for-loop. Taking this code, why is
I want to process a file line by line in c, all lines in
I've got this XML: <BillingLog> <BillingItem> <date-and-time>2003-11-04</date-and-time> <application-name>Billing Service</application-name> <severity>Warning</severity> <process-id>123</process-id> <description>Timed out on
i want to know the height of all items my StackPanel . What is
Let this be the xml: ... some other parent elements here ... <subject group=a>Economy</subject>
While trying to process and obtain data from an XML document, I want to
Possible Duplicate: Algorithm to return all combinations of k elements from n Generate Distinct
I want to process an array from java script. (i.e., from html page to
I want to process MSXML which is generated by Visio 2010, Which Language will

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.