C++11为容器增加了emplace_back成员函数,改函数可以改善push_back的效率:
#include <iostream>
#include <vector>
using namespace std;
class A{
public:
A(int i)
{
cout<<"A construct, this:"<<this<<endl;
}
A(const A &a)
{
cout<<"A copy construct, this:"<<this<<" ori:"<<&a<<endl;
}
~A()
{
cout<<"A deconstruct, this:"<<this<<endl;
}
};
int main() {
vector<A> v;
v.push_back(1);
return 0;
}
运行程序输出:
A construct, this:0x61fe0f
A copy construct, this:0xd26190 ori:0x61fe0f
A deconstruct, this:0x61fe0f
A deconstruct, this:0xd26