std::remove_if()
is a bit deceiving, because it doesn't actually remove anything. Rather, it moves all the values not meeting the removal criteria toward the beginning of the container, overwriting the ones that do meet the removal criteria. It returns an iterator pointing at the first "removed" entry. But the entries from that point on are not necessarily the removed values; their contents are unspecified.
So you need to learn about the Erase Remove Idiom. Here's an example:
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
void filter(std::vector<int>& vec)
{
vec.erase(std::remove_if(begin(vec), end(vec),
[](int n) { return n < 0 || n > 25; }),
end(vec));
}
int main()
{
std::vector<int> vec = {27, 1, 9, 6, 93, 73, 22, 25, 0, 18, 73};
std::cout << "Original: ";
std::copy(begin(vec), end(vec), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
filter(vec);
std::cout << "Filtered: ";
std::copy(begin(vec), end(vec), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
This erases all the entries from the return value of std::remove_if()
to the end of the container. Then they actually are removed. The output is:
Original: 27 1 9 6 93 73 22 25 0 18 73
Filtered: 1 9 6 22 25 0 18
Note also that your comparison was incorrect and would always yield false
, because there is no number that is both less than 0 and greater than 25.