class A
{
public:
A():a(0)
{}
A(int x):a(x)
{
cout<<"convert"<<endl;
}
A(const A& rhs):a(rhs.a)
{
cout<<"copy: "<<a<<endl;
}
void print()
{
cout<<a<<endl;
}
void Set(int x)
{
a=x;
}
private:
int a;
};
int main()
{
vector<A>vec2(2,A(100));
cout<<"the size: "<<vec2.size()<<" the capacity: "<<vec2.capacity()<<endl;
vec2.push_back(17);
for(int i=0; i<vec2.capacity();i++)
{
vec2[i].print();
}
cout<<"the size: "<<vec2.size()<<" the capacity: "<<vec2.capacity()<<endl;
}
转变 副本:100 副本:100 尺寸:2 容量:2 转变 副本:17 副本:100 副本:100 100 100 17 0
为什么会这样
copy: 17
copy: 100
copy: 100
似乎容量是 5 而不是 4,并且在我要推送的元素推入向量后容量增加了,我一定是错的,有人可以告诉我更多细节吗?