How to remove elements from a list based on condition in C++

1 Answer

0 votes
#include <iostream>
#include <list>
#include <algorithm>

using std::cout;
using std::endl;
using std::list;

int main()
{
	list<int> lst{ 1,2,3,4,5,6,7,8,9 };

	lst.remove_if([](const int & n) {
		if ((n % 2) == 0)
			return true;
		else
			return false;
	});

	for (int val : lst)
		cout << val << " ";

	cout << endl;

	return 0;
}


/*
run:

1 3 5 7 9

*/

 



answered Apr 28, 2018 by avibootz

Related questions

1 answer 218 views
1 answer 226 views
1 answer 227 views
1 answer 157 views
...