1

I know how we can remove elements from a vector of int

std::vector<int> vec; // .. put in some values .. int int_to_remove = n; vec.erase(std::remove(vec.begin(), vec.end(), int_to_remove), vec.end()); 

What if its a vector<obj> vec where obj is

class obj { int ID; string name; } 

How would I remove vectors that are holding onto a certain ID ?

std::vector<obj> vec; // .. put in some values .. int id_to_remove = n; vec.erase(std::remove(vec.ID.begin(), vec.ID.end(), id_to_remove), vec.end()); 
1
  • remove calls destructor Commented Aug 18, 2016 at 11:16

1 Answer 1

2

Now that you are looking to delete objects matching a certain criteria, you need to use std::remove_if instead of std::remove.

vec.erase( std::remove_if( vec.ID.begin() , vec.ID.end() , [](const obj& x) { // ID needs to be public in order for this to compile return x.ID == id_to_remove; } ) , vec.end() ); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.