-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto.cpp
75 lines (59 loc) · 1.06 KB
/
auto.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
64
65
66
67
68
69
70
71
72
73
74
75
#include <vector>
#include <iostream>
// https://stackoverflow.com/questions/15927033/what-is-the-correct-way-of-using-c11s-range-based-for
// A sample test class, with custom copy semantics.
class X {
public:
X()
: m_data(0)
{}
X(int data)
: m_data(data)
{}
~X()
{}
X(const X& other)
: m_data(other.m_data)
{
std::cout << "X copy ctor.\n";
}
X& operator=(const X& other)
{
m_data = other.m_data;
std::cout << "X copy assign.\n";
return *this;
}
int Get() const
{
return m_data;
}
private:
int m_data;
};
std::ostream& operator<<(std::ostream& os, const X& x)
{
os << x.Get();
return os;
}
int main()
{
{
std::vector<int> v = {1, 3, 5, 7, 9};
for (auto x : v) {
std::cout << x << ' ';
}
std::cout << std::endl;
}
{
std::vector<X> v = {1, 3, 5, 7, 9};
std::cout << "\nElements:\n";
for (auto x : v) {
std::cout << x << ' ';
}
std::cout << std::endl;
for (const auto& x : v) {
std::cout << x << ' ';
}
std::cout << std::endl;
}
}