尝试着自己写一点练习,但母语为Java的我对于C++似乎还是有点不适应
还是很微妙呀,例如容器在堆上的行为,在栈上的行为,混合时的行为,delete的行为等
做了个研究,写了点代码,不过其中还是有点疑惑
当一个对象放在栈上的时候,对象包含的vector对象会只保留一个地址,这个地址指向一个与堆/栈均不同的位置。
这个位置在哪里?已知的是比堆要高很多
上代码
1
#ifndef PERSON_H
2 #define PERSON_H
3
4 #include < iostream >
5 #include < string >
6 #include < vector >
7
8 using namespace std;
9
10 class Person
11 {
12public:
13 Person(string name,int size);
14 string getName();
15 void setName(string name);
16 virtual string getType();
17 vector <string> family;
18private:
19 string name;
20} ;
21
22 class Wife : public Person {
23public:
24 Wife(string name,int year);
25 int getYear();
26 virtual string getType();
27protected:
28private:
29 int year;
30} ;
31
32 #endif
2 #define PERSON_H
3
4 #include < iostream >
5 #include < string >
6 #include < vector >
7
8 using namespace std;
9
10 class Person
11 {
12public:
13 Person(string name,int size);
14 string getName();
15 void setName(string name);
16 virtual string getType();
17 vector <string> family;
18private:
19 string name;
20} ;
21
22 class Wife : public Person {
23public:
24 Wife(string name,int year);
25 int getYear();
26 virtual string getType();
27protected:
28private:
29 int year;
30} ;
31
32 #endif
1
#include
"
Person.h
"
2 #include < cstring >
3 #include < sstream >
4
5
6 Person::Person( string name, int size)
7 {
8 this->name = name;
9 this->family = vector<string>(size);
10}
11 string Person::getName() {
12 return this->name;
13}
14 string Person::getType() {
15 return "Person";
16}
17
18 void Person::setName( string name) {
19 this->name = name;
20}
21 Wife::Wife( string name, int year) :Person(name, 0 )
22 {
23 this->year = year;
24}
25 int Wife::getYear() {
26 return this->year;
27}
28 string Wife::getType() {
29 return "Wife";
30}
31
32
33 int main() {
34 //堆内存分配示例
35 Person *p = new Person("Gan",0);
36 //vector容器示例
37
38 //注意:值复制,不能这样得到并修改p的family
39 //vector<string> this_family = p->family;
40
41 //应该用指针或引用
42 vector<string> &this_family = p->family;
43
44 //向family增加元素后,family到底是放到了栈上还是堆上?
45 //p在堆上时,vector也在堆上
46 //vector始终保持连续
47 //与Vector相同位置?
48 //p在栈上时,vector不在栈上,在另一个可能的堆上或者是栈上
49 //但是与p在堆上时的情况不同
50 //释放p之后,family会被释放吗?
51 //全部都会
52
53 //注意,如果vector初始容量不为0,则push_back方法会从初始容量之后加入元素
54 this_family.push_back("Yin");
55 this_family.push_back("Chen");
56 this_family.push_back("Hui");
57 this_family.push_back("END");
58
59 //vector迭代器示例
60 vector<string>::iterator pos = p->family.begin();
61 vector<string>::iterator end = p->family.end();
62 //测试p->family 与 this_family 是否指向同一对象
63 cout << (pos == end) << endl;
64 //++pos 效率在某些情况下比pos++高
65 for (;pos != end; ++pos){
66 cout << pos->substr(1,2) << endl;
67 //vector 迭代器搜索
68 if (*pos == "Chen"){
69 //必须更新所有迭代器,否则pos,end变成野指针
70 //如果在for循环中每次都取得end,则更好
71 vector<string>::iterator new_pos = p->family.erase(pos);
72 //pos已经变成野指针
73 //更新
74 pos = new_pos;
75 end = p->family.end();
76 }
77 }
78
79 //继承示例
80 Wife *mywife = new Wife("Yin Chenhui",2007);
81 cout << "Year: " << mywife->getYear() << endl;
82
83 //多态示例
84 vector <Person*> *people = new vector<Person*>();
85 people->push_back(p);
86 people->push_back(mywife);
87
88 vector<Person*>::iterator pos2;
89 int i = 0;
90 for (pos2 = people->begin();pos2 !=people->end();pos2++){
91 //多态方法
92 cout << "Person vector: " << i << ' '<< (*pos2)->getName()
93 << ' ' << (*pos2)->getType() << endl;
94 ++i;
95 }
96 delete people;
97 delete mywife;
98 delete p;
99
100 //栈分配
101 Person gangan("gangan",0);
102 gangan.family.push_back("1gangan1");
103 gangan.family.push_back("2gangan2");
104 gangan.family.push_back("3gangan3");
105
106 cout << "gangan's Address: " << &gangan << endl;
107}
2 #include < cstring >
3 #include < sstream >
4
5
6 Person::Person( string name, int size)
7 {
8 this->name = name;
9 this->family = vector<string>(size);
10}
11 string Person::getName() {
12 return this->name;
13}
14 string Person::getType() {
15 return "Person";
16}
17
18 void Person::setName( string name) {
19 this->name = name;
20}
21 Wife::Wife( string name, int year) :Person(name, 0 )
22 {
23 this->year = year;
24}
25 int Wife::getYear() {
26 return this->year;
27}
28 string Wife::getType() {
29 return "Wife";
30}
31
32
33 int main() {
34 //堆内存分配示例
35 Person *p = new Person("Gan",0);
36 //vector容器示例
37
38 //注意:值复制,不能这样得到并修改p的family
39 //vector<string> this_family = p->family;
40
41 //应该用指针或引用
42 vector<string> &this_family = p->family;
43
44 //向family增加元素后,family到底是放到了栈上还是堆上?
45 //p在堆上时,vector也在堆上
46 //vector始终保持连续
47 //与Vector相同位置?
48 //p在栈上时,vector不在栈上,在另一个可能的堆上或者是栈上
49 //但是与p在堆上时的情况不同
50 //释放p之后,family会被释放吗?
51 //全部都会
52
53 //注意,如果vector初始容量不为0,则push_back方法会从初始容量之后加入元素
54 this_family.push_back("Yin");
55 this_family.push_back("Chen");
56 this_family.push_back("Hui");
57 this_family.push_back("END");
58
59 //vector迭代器示例
60 vector<string>::iterator pos = p->family.begin();
61 vector<string>::iterator end = p->family.end();
62 //测试p->family 与 this_family 是否指向同一对象
63 cout << (pos == end) << endl;
64 //++pos 效率在某些情况下比pos++高
65 for (;pos != end; ++pos){
66 cout << pos->substr(1,2) << endl;
67 //vector 迭代器搜索
68 if (*pos == "Chen"){
69 //必须更新所有迭代器,否则pos,end变成野指针
70 //如果在for循环中每次都取得end,则更好
71 vector<string>::iterator new_pos = p->family.erase(pos);
72 //pos已经变成野指针
73 //更新
74 pos = new_pos;
75 end = p->family.end();
76 }
77 }
78
79 //继承示例
80 Wife *mywife = new Wife("Yin Chenhui",2007);
81 cout << "Year: " << mywife->getYear() << endl;
82
83 //多态示例
84 vector <Person*> *people = new vector<Person*>();
85 people->push_back(p);
86 people->push_back(mywife);
87
88 vector<Person*>::iterator pos2;
89 int i = 0;
90 for (pos2 = people->begin();pos2 !=people->end();pos2++){
91 //多态方法
92 cout << "Person vector: " << i << ' '<< (*pos2)->getName()
93 << ' ' << (*pos2)->getType() << endl;
94 ++i;
95 }
96 delete people;
97 delete mywife;
98 delete p;
99
100 //栈分配
101 Person gangan("gangan",0);
102 gangan.family.push_back("1gangan1");
103 gangan.family.push_back("2gangan2");
104 gangan.family.push_back("3gangan3");
105
106 cout << "gangan's Address: " << &gangan << endl;
107}