-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.cpp
45 lines (38 loc) · 843 Bytes
/
vector.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
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
// STLvector类的学习
struct Stu
{
int age;
std::string name;
Stu(int age, std::string name) : age(age), name(name) {}
Stu(const Stu &stu) : age(stu.age), name(stu.name)
{
std::cout << "Copy" << std::endl;
}
~Stu()
{
std::cout << "Delete" << std::endl;
}
};
// Stu << 打印 运算符重载
std::ostream &operator<<(std::ostream &os, const Stu &stu)
{
os << "age: " << stu.age << " name: " << stu.name;
return os;
}
int main()
{
std::vector<Stu> v;
v.reserve(3);
v.emplace_back(18, "Zhang San");
v.emplace_back(19, "Li Si");
v.emplace_back(20, "Wang Wu");
std::vector<Stu>::iterator it = v.begin();
for (Stu &val : v)
{
std::cout << val << std::endl;
}
}