forked from Shivam4747/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunion.cpp
More file actions
41 lines (33 loc) · 962 Bytes
/
Copy pathunion.cpp
File metadata and controls
41 lines (33 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <algorithm> // std::set_union, std::sort
#include <iostream> // std::cout
#include <vector> // std::vector
// Driver code
int main()
{
int first[] = { 5, 10, 15, 20, 25 };
int second[] = { 50, 40, 30, 20, 10 };
int n = sizeof(first) / sizeof(first[0]);
// Print first array
std::cout << "First array contains :";
for (int i = 0; i < n; i++)
std::cout << " " << first[i];
std::cout << "\n";
// Print second array
std::cout << "Second array contains :";
for (int i = 0; i < n; i++)
std::cout << " " << second[i];
std::cout << "\n\n";
std::vector<int> v(10);
std::vector<int>::iterator it, st;
std::sort(first, first + n);
std::sort(second, second + n);
// Using default function
it = std::set_union(first, first + n, second,
second + n, v.begin());
std::cout << "The union has " << (it - v.begin())
<< " elements:\n";
for (st = v.begin(); st != it; ++st)
std::cout << ' ' << *st;
std::cout << '\n';
return 0;
}