Article Directory
1 Behavior
1. Every time an object is added to the Vector, a round of calling the copy constructor will be performed to create a new object;
2. If the capacity is insufficient when the object is added
(1) Create a new object through copy construction first;
(2) Perform expansion and migration of the original object ;
(3) Add the new object created in step (1);
3. If the capacity is not specified when the vector is initialized, each time an object is added, the capacity and the number of elements will increase by 1 from 0;
2 Vector method of adding objects 1
2.1 Example
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class A
{
private:
int value;
public:
A() {
value = 0;cout << "A() this= " << this << ",value="<<value<<endl; }
A(const A& a) {
cout << "A(A&) this= " << this << endl;
this->value = a.value;
}
void setValue(int value) {
this->value = value; }
int getValue() {
return this->value; }
};
int main()
{
vector<A> v;
cout << endl << "start create object of A:" << endl;
A a1, a2, a3;
a1.setValue(1);
a2.setValue(2);
a3.setValue(3);
cout << endl << "start push:" << endl;
cout << endl << "start push a1:" << endl;
v.push_back(a1);
cout << endl << "start push a2:" << endl;
v.push_back(a2);
cout << endl << "start push a3:" << endl;
v.push_back(a3);
cout << endl<<"start setValue:" << endl;
a1.setValue(4);
a2.setValue(5);
a3.setValue(6);
cout << endl << "start for:" << endl;
for (auto & a : v)
{
cout << &a << ",value="<<a.getValue()<<endl;
}
}
2.2 Operation
start create object of A:
A() this= 00AFFDE8,value=0
A() this= 00AFFDDC,value=0
A() this= 00AFFDD0,value=0
start push:
start push a1:
A(A&) this= 00C160E8
start push a2:
A(A&) this= 00C1B46C
A(A&) this= 00C1B468
start push a3:
A(A&) this= 00C1B4A8
A(A&) this= 00C1B4A0
A(A&) this= 00C1B4A4
start setValue:
start for:
00C1B4A0,value=1
00C1B4A4,value=2
00C1B4A8,value=3
Remarks: After
Vector is initialized, after
adding a1, after
adding a2, after
adding a3
3 Vector adding object method 2 (specified capacity)
3.1 Example
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class A
{
private:
int value;
public:
A() {
value = 0;cout << "A() this= " << this << ",value="<<value<<endl; }
A(const A& a) {
cout << "A(A&) this= " << this << endl;
this->value = a.value;
}
void setValue(int value) {
this->value = value; }
int getValue() {
return this->value; }
};
int main()
{
vector<A> v;
v.reserve(5);
cout << endl << "start create object of A:" << endl;
A a1, a2, a3;
a1.setValue(1);
a2.setValue(2);
a3.setValue(3);
cout << endl << "start push:" << endl;
cout << endl << "start push a1:" << endl;
v.push_back(a1);
cout << endl << "start push a2:" << endl;
v.push_back(a2);
cout << endl << "start push a3:" << endl;
v.push_back(a3);
cout << endl<<"start setValue:" << endl;
a1.setValue(4);
a2.setValue(5);
a3.setValue(6);
cout << endl << "start for:" << endl;
for (auto & a : v)
{
cout << &a << ",value="<<a.getValue()<<endl;
}
}
3.2 Operation
start create object of A:
A() this= 00EFF840,value=0
A() this= 00EFF834,value=0
A() this= 00EFF828,value=0
start push:
start push a1:
A(A&) this= 011B60E8
start push a2:
A(A&) this= 011B60EC
start push a3:
A(A&) this= 011B60F0
start setValue:
start for:
011B60E8,value=1
011B60EC,value=2
011B60F0,value=3