How to copy based on condition elements from int array into a vector in C++

1 Answer

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

using std::cout;
using std::endl;
using std::vector;

bool IsEven(int n) { return ((n % 2) == 0); }

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9 };
	vector<int> vec(9);

	std::remove_copy_if(arr, arr + 9, vec.begin(), IsEven);

	for (vector<int>::iterator p = vec.begin(); p != vec.end(); p++)
		cout << *p << ' ';
	
	cout << endl;

	return 0;
}


/*
run:

1 3 5 7 9 0 0 0 0

*/

 



answered Apr 28, 2018 by avibootz

Related questions

1 answer 232 views
1 answer 220 views
1 answer 184 views
2 answers 199 views
199 views asked Jun 24, 2021 by avibootz
...