我正在尝试使用remove_if
删除向量元素。但是没有成功。我做错了什么?
以下是我的代码:
#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);
}
我的输出结果只有:
1 2 3 4 5 6
期望的输出结果是:
1 2 3 4 5 6 1 3 4 5 6