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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:50:47+00:00 2026-06-16T22:50:47+00:00

I try to remove objects from a vector using vector::erase and std::remove_if. I have

  • 0

I try to remove objects from a vector using vector::erase and std::remove_if. I have an external namespace which does the selection ala:

template<unsigned int value, someType collection>
bool Namespace::isValid(const Foo* object){

   do something
}

Now I have a vector which contains some element which I want to filter if the are valid. In order to do so I do:

 std::vector<foo*> myVector;
 //fill it
 myVector.erase( std::remove_if(myVector.begin(), myVector.end(), Namespace::isValid<myValue, myCollectionType>), myVector.end());

Now this works fine and removes all valid candidates, but actually I want to keep and remove all other. Hence, I need to negate the predicate. Is there any way to do so? Unfortunately, C++11 is not currently supported in this context.

Thanks

  • 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-16T22:50:48+00:00Added an answer on June 16, 2026 at 10:50 pm

    Use the std::not1 adapter to negate the value returned by your isValid function. Note that std::not1 expects a function object (C++ calls this a “functor”), so if you have a plain function in a name space you will also need std::ptr_fun to create a function object out of your function.

    So,

    myVector.erase( std::remove_if( myVector.begin(),
                                    myVector.end(),
                                    Namespace::isValid<myValue, myCollectionType> ),
                    myVector.end() );
    

    becomes

    myVector.erase( std::remove_if( myVector.begin(),
                                    myVector.end(),
                                    std::not1( std::ptr_fun( Namespace::isValid<myValue, myCollectionType> ) ) ),
                    myVector.end() );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have: var myObjects = new ConcurrentBag<object>(); And try to remove objects via:
When I try to remove my PHP extensions from all my files using my
I kinda stumbled into the error where you try to remove objects from an
So, if I try to remove elements from a Java HashSet while iterating, I
I am using the Matisse DB with .NET bindings to fetch objects from the
Possible Duplicate: Remove duplicates from an array of objects in javascript var arr =
I have a LinkedList of objects im trying to iterate over(using an iterator), see
So, im using this code to remove a couple of movie clips from my
I have a list of objects arranged on a grid. I want to remove
I currently have an arraylist in which I add blossom objects ( which fall

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.