how to removing an item in a vector when you dont know where it is.

So I want to remove an item in a vector. I dont know where in the vector it is I just want to remove it. I have looked around and vectors have a lot of nice functionality. what I am asking is,

is there a way that is better than to run a loop that checks vector
if the vector at location i is == items
remove vector at i.

1
2
3
4
5
6
7
8
9

for(int i = 0; i < myvector.size(); i++)
{
if(myvector.at(i) == item)
{
myvector.erase (myvector.begin()+i);
}
}    
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/algorithm/find/
Erase-remove idiom https://en.wikipedia.org/wiki/Erase-remove_idiom

1
2
template< typename CNTR > void remove( CNTR& cntr, const typename CNTR::value_type& item )
{ cntr.erase( std::remove( std::begin(cntr), std::end(cntr), item ), std::end(cntr) ) ; }

http://coliru.stacked-crooked.com/a/cc7cab34d988d43d
Topic archived. No new replies allowed.