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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:48:38+00:00 2026-05-11T02:48:38+00:00

Say we’ve got: struct IsEven { bool operator() (int i) { return i %

  • 0

Say we’ve got:

struct IsEven {    bool operator() (int i) { return i % 2 == 0; } }; 

Then:

vector<int> V; // fill with ints vector<int>::iterator new_end = remove_if(V.begin(), V.end(), IsEven()); V.erase(new_end, V.end()); 

works fine (it leaves V with only the odd integers). But it seems that the elements from new_end to V.end() are not the even integers that we’re deleting. For example, if v starts out as 1 4 2 8 5 7, then I’m getting 8 5 7 for those elements (although after the erase call, the vector indeed has 1 5 7 left).

Apparently, (according to http://www.sgi.com/tech/stl/remove_if.html)

The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. 

First of all, WTF? And second, how do I get around this without essentially reimplementing remove_if?

  • 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. 2026-05-11T02:48:38+00:00Added an answer on May 11, 2026 at 2:48 am

    It sounds like you want to use partition() to partition the vector into groups of odd values at the start and even values at the end. partition() will return an iterator to the first element of the second grouping.

    As for the WTF, I’m not sure why you would expect a remove operation to preserve the elements you want to remove by copying them (that’s extra work) to the end of the container. Most people consider the WTF in remove() (and it’s cousins) to be the fact that the size of the vector is not reduced and you have to call erase() to actually delete undesired elements after the remove operation.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

Say we have the following method: private MyObject foo = new MyObject(); // and
Say we have realized a value of TDD too late. Project is already matured,
Say we have: class Base { virtual void f() {g();}; virtual void g(){//Do some
Say we have a TestClass with the 2 methods, -getSomeString and -getAnotherString , and
Say we've got: struct IsEven { bool operator() (int i) { return i %

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.