emplace_back和push_back区别
#include <vector>?
#include <iostream>?
using namespace std;
class testDemo
{
public:
?? ?testDemo(int num) :num(num) {
?? ??? ?std::cout << "調用構造函數" << endl;
?? ?}
?? ?testDemo(const testDemo& other) :num(other.num) {
?? ??? ?std::cout << "調用拷貝構造函數" << endl;
?? ?}
?? ?testDemo(testDemo&& other) :num(other.num) {
?? ??? ?std::cout << "調用移動構造函數" << endl;
?? ?}
private:
?? ?int num;
};
int main()
{
?? ?cout << "emplace_back:" << endl;
?? ?std::vector<testDemo> demo1;
?? ?demo1.emplace_back(2);
?? ?cout << "push_back:" << endl;
?? ?std::vector<testDemo> demo2;
?? ?testDemo demo(2);
?? ?demo2.push_back(std::move(demo));
}
區別:
?
總結
以上是生活随笔為你收集整理的emplace_back和push_back区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抛出错误_不用try catch,如何机
- 下一篇: 大学电路题目怎么搜_长沙理工大学2020