This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_swap.cpp
63 lines (60 loc) · 1.48 KB
/
vector_swap.cpp
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
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
using namespace std;
void vector_swap(vector<int> &v1, vector<int> &v2,
int start1, int end1,
int start2, int end2)
{
int d1 = end1 - start1, d2 = end2 - start2;
int ns = min(d1, d2);
for (int i = 0; i < ns; ++i)
{
swap(v1[start1 + i], v2[start2 + i]);
}
if (d1 > d2)
{
v2.insert(v2.begin() + start2 + ns, v1.begin() + start1 + ns, v1.begin() + start1 + d1);
v1.erase(v1.begin() + start1 + ns, v1.begin() + start1 + d1);
}
else if (d2 > d1)
{
v1.insert(v1.begin() + start1 + ns, v2.begin() + start2 + ns, v2.begin() + start2 + d2);
v2.erase(v2.begin() + start2 + ns, v2.begin() + start2 + d2);
}
}
int main()
{
// read input
int n, c;
vector<int> v1, v2;
cin >> n; // number of v1
for (int i = 0; i < n; i++)
{
cin >> c;
v1.push_back(c);
}
cin >> n; // number of v2
for (int i = 0; i < n; i++)
{
cin >> c;
v2.push_back(c);
}
int s1, e1, s2, e2; // position
cin >> s1 >> e1 >> s2 >> e2;
// call the function
vector_swap(v1, v2, s1, e1, s2, e2);
// display content of the stack
cout << "v1 has " << v1.size() << endl;
for (auto &x : v1)
{
cout << x << " ";
}
cout << endl;
// display content of the stack
cout << "v2 has " << v2.size() << endl;
for (auto &x : v2)
{
cout << x << " ";
}
cout << endl;
}