vector删除的简洁写法

#include <iostream>
#include <vector>
#include <algorithm>

struct Test
{
	int a;
	int b;
	Test(int _a, int _b) { a = _a; b = _b; }
};

bool Is_2(Test t) { return (2 == t.a); }

int main()
{
	std::vector<Test> vec;
	Test t1(1, 1);
	Test t2(2, 2);
	Test t3(3, 3);
	Test t4(4, 4);
	Test t5(5, 5);

	vec.push_back(t1);
	vec.push_back(t2);
	vec.push_back(t3);
	vec.push_back(t4);
	vec.push_back(t5);
	
	for (auto iter = vec.begin(); iter != vec.end(); iter++)
	{

		vec.erase(remove_if(vec.begin(), vec.end(), Is_2), vec.end());
		std::cout << iter->a;
	}
	

	while (1);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_23350817/article/details/103390226