Answer by Chris Drew for Erase some of a vector's elements in a for-each loop...
As @BenjaminLindley and @JMerdich pointed out, for the problem stated, a two-pass approach is probably simpler and more efficient. In a realistic situation, it is possible that there is some expensive...
View ArticleAnswer by J. Merdich for Erase some of a vector's elements in a for-each loop...
I was curious about the performance of this, so I ran a quick naive benchmark comparing Benjamin's find then partial clean and hnefatl's for-loop. Benjamin's is indeed faster: 113x faster. Impressive....
View ArticleAnswer by Benjamin Lindley for Erase some of a vector's elements in a...
You should still use std::remove_if, just call std::find beforehand.auto el = std::find(vec.begin(), vec.end(), whatImLookingFor);auto p = std::remove_if(vec.begin(), el, isInvalid);// returns the...
View ArticleAnswer by hnefatl for Erase some of a vector's elements in a for-each loop...
This is an intuitive approach to the problem that keeps the looping structure - while it only performs a single pass, due to repeated erasing it's likely to be less efficient than a two-pass approach....
View ArticleAnswer by Ulrich Eckhardt for Erase some of a vector's elements in a for-each...
If a simple loop, exited with break is too primitive for you, I'd suggest using std::find() to get an iterator to the searched element and then to use vector.erase()to delete the othere elements.
View ArticleErase some of a vector's elements in a for-each loop without iterating the...
I have a vector and I am searching for an element in it while iterating over the vector with a for-each loop. If I find any invalid elements during the search, I would like to remove them from the...
View Article
More Pages to Explore .....