Removing elements from vector using remove_if
Removing elements from vector using remove_if
I am trying to remove vector elements using remove_if. But unsuccessfully. What am I doing wrong?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
void printme(std::vector<int>& a){
for(const auto& item: a)
std::cout << item << std::endl;
}
int main()
{
std::vector<int> a {1, 2, 3, 4, 5, 6};
printme(a);
a.erase( (std::remove_if(a.begin(), a.end(), (const int& x){
return x == 2;
}), a.end()));
printme(a);
}
My output is just: 1 2 3 4 5 6
Expected output: 1 2 3 4 5 6 1 3 4 5 6
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.