#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
using namespace std;class student {
public:student() {cout << "无参构造函数被调用!" << endl;}student(int age, string name) {this->age = age;//strncpy_s(this->name, name, 64);cout << "有参构造函数被调用!" << endl;cout << "姓名:" << name.c_str() << " 年龄:" << age << endl;}student(const student& s) {this->age = s.age;//strncpy_s(this->name, s.name, 64);cout << "拷贝构造函数被调用!" << endl;}~student() {cout << "析构函数被调用" << endl;}
public:int age;string name;
};int main(void)
{vector<student> vectStu;//插入学生到容器中 先定义再插入student xiaohua(10, "李晓华");vectStu.push_back(xiaohua);return 0;}
这样写代码 再插入调用
...int main(void)
{vector<student> vectStu;//直接插入临时对象vectStu.push_back(student(19, "王大锤"));return 0;}
直接插入临时对象
两种方法都进行了拷贝,效率较低。所以c++11提供了变参模板和完美转发
int main(void)
{vector<student> vectStu;//变参模板和完美转发vectStu.emplace_back(19,"王大锤");return 0;}