forked from SilverMaple/STLSourceCodeNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_4_3_copy-overlap.cpp
More file actions
80 lines (64 loc) · 1.97 KB
/
6_4_3_copy-overlap.cpp
File metadata and controls
80 lines (64 loc) · 1.97 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// file: 6copy-overlap.cpp
#include <iostream>
#include <algorithm>
#include <deque>
using namespace std;
template <class T>
struct display {
void operator()(const T& x){
cout << x << " ";
}
};
int main() {
{
int ia[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
// 输出区间终点与输入区间重叠
copy(ia + 2, ia + 7, ia);
for_each(ia, ia + 9, display<int>());
cout << endl;
}
{
int ia[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
// 输出区间起点与输入区间重叠
copy(ia + 2, ia + 7, ia+4);
for_each(ia, ia + 9, display<int>());
cout << endl;
// 本例正确,是因为copy使用memmove执行实际复制
}
{
int ia[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
deque<int> id(ia, ia + 9);
auto first = id.begin();
auto last = id.end();
++++first; // advance(first, 2);
cout << *first << endl;
----last;
cout << *last << endl;
auto result = id.begin();
cout << *result << endl;
// 输出区间终点与输入区间重叠
copy(first, last, result);
for_each(id.begin(), id.end(), display<int>());
cout << endl;
}
{
int ia[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
deque<int> id(ia, ia + 9);
auto first = id.begin();
auto last = id.end();
++++first; // advance(first, 2);
cout << *first << endl;
----last;
cout << *last << endl;
auto result = id.begin();
advance(result, 4);
cout << *result << endl;
// 输出区间起点与输入区间重叠
copy(first, last, result);
for_each(id.begin(), id.end(), display<int>());
cout << endl;
// 本例结果错误,是因为copy不再使用memmove执行实际复制
// 如果换成vector结果正确,是因为vector迭代器时原生指针,
// copy算法可以调用memmove执行实际复制
}
}