How to copy only numbers that are present in two int arrays into third array in C++

1 Answer

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

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

#define SIZE 10

int main()
{
	int arr1[SIZE] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	int arr2[] = { 2, 5, 6, 9 };
	int len2 = sizeof(arr2) / sizeof(int);
	
	std::ostream_iterator<int> output(cout, " ");

	int intersection[SIZE];

	int *p = std::set_intersection(arr1, arr1 + SIZE, arr2, arr2 + len2, intersection);

	std::copy(intersection, p, output);

	cout << endl;

	return 0;
}

/*
run:

2 5 6 9

*/

 



answered Mar 2, 2018 by avibootz
...