#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
class Rock
{
public:
Rock(){cout<<"Rock()"<<endl;}
Rock(const Rock & rock){cout<<"Rock(Rock & rock)"<<endl;}
Rock & operator=(const Rock & rock){cout<<"operator = "<<endl;return *this;}
~Rock(){cout<<"~Rock"<<endl;}
};
int main()
{
vector<Rock> rock;
Rock *rc1 = new Rock[20];
for (int i = 0; i < 20; i++)
{
cout<<i<<": ";
rock.push_back(rc1[i]);
}
delete [] rc1;
system("pause");
return 0;
}
注意:用push_back往vector里添加一个对象,会调用拷贝构造函数,发现已申请的空间不够时,会申请一个更大的空间(用来保存对象),把原来的对象复制过来,销毁原来的对象。