I have a vector of int
vector<int> p;
now I want to delete one of its items that is equal to 3.
there is no remove like p.remove(3)
but there is a erase but at first I should I find it.
there were two questions about this available in stackoverflow. both of them said that we should find it by
std::remove(p.begin(), p.end(), 3)
but this code isn’t compiled. it said function does not take 3 arguments.
Use Erase Remove Idiom .
Vectors
erasemethod takes one or two iterators. The first one, erases everthing from the specified position to the end.std::removefrom<algorithm>moves all matching elements to the end of the sequence and returns an iterator to the position of the new end. You can use this iterator forerase.If you want to remove the first matching element, use
std::findto retrieve an iterator to the first element and pass that toerase.