how to remove all occurance from vector<string>

I have vector in class declaration which holds strings.

vector<string> which contains strings like
"test1"
"test2"
"test4"
"test1"
"test1"

In this case, I would like to remove all occurrence of "test1" from vector.
like, RemovefromVector("test1") results in (vector contains now only)
"test2"
"test4"

Can anyone please help me out.
Thanks in advance.
You want the remove algorithm. It's described in the Reference section, under STL Algorithms. It returns an iterator to the new end of the container (algorithms cannot actually erase elements).

Example:
1
2
3
4
5
6
7
8
#include <algorithm>
//...
vector<string> v;
v.push_back( "test1" );
v.push_back( "test2" );
v.push_back( "test3" );
//...
v.erase( remove( v.begin(), v.end(), "test2" ), v.end() );

Last edited on
Thank you so much.

I am also new to STL and I would like to use STL with user defined classes.
Can you please suggest some ways so I can have nice grip over use of STL.

Thanks.
Effective STL by Scott Meyers is an excellent start; I recommend going that route. You can pick up a used one online at a good price and it's a short read, too!
Thanks you so much everyone ..
really appreciate your help and guidance ...

Topic archived. No new replies allowed.