.size() is the number of elements that are contained in the vector, whereas .capacity() is the number of elements that can be added to the vector, before memory will be re-allocated.
C++ vectors do not support in-place reallocation of memory
Initial Size: 0, Capacity: 1 ----------------------------------------------------- Constructor of CInfo, address: 0000019A89325B50 Size: 1, Capacity: 1 ----------------------------------------------------- Constructor of CInfo, address: 0000019A89325658 Destructor of CInfo, address: 0000019A89325B50 Size: 2, Capacity: 2 ----------------------------------------------------- Constructor of CInfo, address: 0000019A893248A0 Destructor of CInfo, address: 0000019A89325650 Destructor of CInfo, address: 0000019A89325658 Size: 3, Capacity: 3 ----------------------------------------------------- Constructor of CInfo, address: 0000019A893245A8 Destructor of CInfo, address: 0000019A89324890 Destructor of CInfo, address: 0000019A89324898 Destructor of CInfo, address: 0000019A893248A0 Size: 4, Capacity: 4 ----------------------------------------------------- Constructor of CInfo, address: 0000019A89320C90 Destructor of CInfo, address: 0000019A89324590 Destructor of CInfo, address: 0000019A89324598 Destructor of CInfo, address: 0000019A893245A0 Destructor of CInfo, address: 0000019A893245A8 Size: 5, Capacity: 6 ----------------------------------------------------- Constructor of CInfo, address: 0000019A89320C98 Size: 6, Capacity: 6 ----------------------------------------------------- Constructor of CInfo, address: 0000019A893190B0 Destructor of CInfo, address: 0000019A89320C70 Destructor of CInfo, address: 0000019A89320C78 Destructor of CInfo, address: 0000019A89320C80 Destructor of CInfo, address: 0000019A89320C88 Destructor of CInfo, address: 0000019A89320C90 Destructor of CInfo, address: 0000019A89320C98 Size: 7, Capacity: 9 ----------------------------------------------------- Constructor of CInfo, address: 0000019A893190B8 Size: 8, Capacity: 9 ----------------------------------------------------- Constructor of CInfo, address: 0000019A893190C0 Size: 9, Capacity: 9 ----------------------------------------------------- Constructor of CInfo, address: 0000019A8931B098 Destructor of CInfo, address: 0000019A89319080 Destructor of CInfo, address: 0000019A89319088 Destructor of CInfo, address: 0000019A89319090 Destructor of CInfo, address: 0000019A89319098 Destructor of CInfo, address: 0000019A893190A0 Destructor of CInfo, address: 0000019A893190A8 Destructor of CInfo, address: 0000019A893190B0 Destructor of CInfo, address: 0000019A893190B8 Destructor of CInfo, address: 0000019A893190C0 Size: 10, Capacity: 13 ----------------------------------------------------- Destructor of CInfo, address: 0000019A8931B050 Destructor of CInfo, address: 0000019A8931B058 Destructor of CInfo, address: 0000019A8931B060 Destructor of CInfo, address: 0000019A8931B068 Destructor of CInfo, address: 0000019A8931B070 Destructor of CInfo, address: 0000019A8931B078 Destructor of CInfo, address: 0000019A8931B080 Destructor of CInfo, address: 0000019A8931B088 Destructor of CInfo, address: 0000019A8931B090 Destructor of CInfo, address: 0000019A8931B098
行,可以看到,每次re-allocation发生,也就是capacity改变,原地址对象都会被析构
我们再实现拷贝构造函数看看(什么也不干,只实现打印信息)
The copy constructor is called whenever an object is initialized (by direct-initialization or copy-initialization) from another object of the same type