Remove items from a stl vector

S

silverburgh.meryl

How can I remove items from a stl vector if it exists on another
vector?

In Java, I can do this:
vector v1 = new Vector();
// populate v1

vector v2 = new Vector();
// populate v2

v1.removeAll(v2);

how can I do that in C++?
Do I have to write my own functor and pass that to remove_if()
algorithm?
 
V

Victor Bazarov

How can I remove items from a stl vector if it exists on another
vector?

Which one do you designate 'it'? The item or the vector?
In Java, I can do this:
vector v1 = new Vector();
// populate v1

vector v2 = new Vector();
// populate v2

v1.removeAll(v2);

how can I do that in C++?

Use 'remove_if' and 'find'.
Do I have to write my own functor and pass that to remove_if()
algorithm?

Yes, your own functor would probably be the best (the clearest). Or you
can sort both vectors (or their copies) and then use 'set_difference'.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top