-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask15.cpp
More file actions
54 lines (43 loc) · 1015 Bytes
/
Copy pathtask15.cpp
File metadata and controls
54 lines (43 loc) · 1015 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
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <set>
using namespace std;
set<int>& fill_set(set<int>& s, int n) {
int num;
for (; n > 0; --n) {
cin >> num;
s.insert(num);
}
return s;
}
set<int> intersection(const set<int>& s1, const set<int>& s2) {
set<int> new_s;
set<int>::const_iterator it1 = s1.begin();
set<int>::const_iterator it2 = s2.begin();
while (it1 != s1.end() && it2 != s2.end()) {
if (*it1 < *it2) {
++it1;
}
else if (*it1 > *it2) {
++it2;
}
else {
new_s.insert(*it1);
++it1;
++it2;
}
}
return new_s;
}
int main() {
set<int> s1, s2;
int n;
cin >> n;
s1 = fill_set(s1, n);
cin >> n;
s2 = fill_set(s2, n);
set<int> s = intersection(s1, s2);
for (set<int>::const_iterator it = s.begin(); it != s.end(); ++it) {
cout << *it << ' ';
}
return 0;
}