STL容器的emplace操作
emplace是c++11引入的成员,作用是优化存储类的vector,deque,list容器
c.emplace_back(构造函数的参数) //向尾部添加一个对象,参数为类的构造函数的参数
c.emplace_front(构造函数的参数)
c.emplace(iteator,构造函数的参数) //向迭代器参数前添加一个对象
例如:
我们新创建一个存储Human类的vector
int main()
{
vector<Human> v;
system("pause");
return 0;
}
//Human头文件
class Human
{
public:
Human(string name = "",int age = 0);
Human(const Human& h);
~Human();
friend ostream& operator<<(ostream& os, Human h);
private:
string name;
int age;
};
ostream& operator<<(ostream& os, Human h);
#include "Human.h"
Human::Human(string name,int age) {
this->name = name;
this->age = age;
cout