VECTOR使用时,经常会遇到要删掉一些不符合条件的元素
remove_if非常适合这个东西的用法
利用比较函数,然后再将元素传入,根据你想要的条件,返回结果为真的数据都会删除
// stl_test.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool is_odd(int tmp)
{
cout << "?" << tmp << endl;
return tmp > 5 ;
}
void test()
{
vector<int> array;
array.push_back(5);
array.push_back(6);
array.push_back(7);
//array.erase(remove_if(array.begin(), array.end(), is_odd), array.end());
//lambda 表达式 去掉大于5的数
// array.erase(remove_if(array.begin(), array.end(), [](int n){ return n > 5; }), array.end());
cout << "=" << endl;
vector<int> ::iterator itor;
for (itor = array.begin(); itor != array.end();)
{
cout << "="<<*itor << endl;
itor++;
}
int i;
cin >> i;
}
int _tmain(int argc, _TCHAR* argv[])
{
test();
return 0;
}