Domanda

I came across an article that stated that the best method of removing items for vectors is using the erase-remove idiom which is as follows

//say vec = 1963,1923,1900,1963,1967;
vector.erase(std::remove(vec.begin(),vec.end(),1963),vec.end());

Now from what I understand is that vec.erase takes in two parameters the first and the last iterator. The range includes all the elements between first and last. I wanted to know how std::remove would give that single iterator range if the value to be removed is present at different indexes in the vector. I want to know how std::remove works with vector.erase

È stato utile?

Soluzione

remove doesn't give a range. It just gives one iterator. The range you pass to erase starts at that iterator and ends at vec.end(), which is your second argument.

remove works by rearranging the sequence so that all the elements to be removed are at the end, of the sequence, and it returns the iterator that points to the first of those to-be-removed elements.

(So remove doesn't actually remove anything - but the content of the to-be-removed elements is indeterminate and should not be accessed anyway.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top