zoukankan      html  css  js  c++  java
  • 3个例子详解C++ 11 中push_back 和 emplace_back差异

    本文首发于个人博客https://kezunlin.me/post/b83bc460/,欢迎阅读最新内容!

    cpp11 push_back and emplace_back

    Guide

    case1

    #include <iostream>
    #include <vector>
    class A
    {
    public:
      A (int x_arg) : x (x_arg) { std::cout << "A (x_arg)
    "; }
      A () { x = 0; std::cout << "A ()
    "; }
      A (const A &rhs) noexcept { x = rhs.x; std::cout << "A (A &)
    "; }
      A (A &&rhs) noexcept { x = rhs.x; std::cout << "A (A &&)
    "; }
      ~A() { std::cout << "~A ()
    "; }
    
    private:
      int x;
    };
    
    void test_emplace_back_1()
    {
    	// For emplace_back constructor A (int x_arg) will be called.
    	// And for push_back A (int x_arg) is called first and 
    	// move A (A &&rhs) is called afterwards
      {
        std::vector<A> a;
        std::cout << "call emplace_back:
    ";
        a.emplace_back(0); 
    	// (1) direct object creation inside vector
      }
    
      {
        std::vector<A> a;
        std::cout << "call push_back:
    ";
        a.push_back(1);
    	// (1) create temp object and 
    	// (2) then move copy to vector and 
    	// (3) free temp object
      }
    }
    /*
    call emplace_back:
    A (x_arg)
    ~A ()
    call push_back:
    A (x_arg)
    A (A &&)
    ~A ()
    ~A ()
     */
    

    see kezunlin

    image from c-difference-between-emplace_back-and-push_back-function

    case2

    void test_emplace_back_2()
    {
    	// emplace_back and push_back for `A(0)`, it's same.
    	// A (int x_arg) is called first and 
    	// move A (A &&rhs) is called afterwards
      {
        std::vector<A> a;
        std::cout << "call emplace_back:
    ";
        a.emplace_back(A(0)); 
    	// (1) create temp object and 
    	// (2) then move copy to vector and 
    	// (3) free temp object
      }
    
      {
        std::vector<A> a;
        std::cout << "call push_back:
    ";
        a.push_back(A(1));
    	// (1) create temp object and 
    	// (2) then move copy to vector and 
    	// (3) free temp object
      }
    }
    
    /*
    call emplace_back:
    A (x_arg)
    A (A &&)
    ~A ()
    ~A ()
    call push_back:
    A (x_arg)
    A (A &&)
    ~A ()
    ~A ()
     */
    

    case 3

    void test_emplace_back_3()
    {
    	// emplace_back and push_back for `A obj(0)`, it's same.
    	// A (int x_arg) is called first and 
    	// copy constructor A (A &) is called afterwards
      {
        std::vector<A> a;
        std::cout << "call emplace_back:
    ";
        A obj(0);
        a.emplace_back(obj); 
    	 // copy constructor to vector
      }
    
      {
        std::vector<A> a;
        std::cout << "call push_back:
    ";
        A obj(1);
        a.push_back(obj);
    	 // copy constructor to vector
      }
    }
    /*
    call emplace_back:
    A (x_arg)
    A (A &)
    ~A ()
    ~A ()
    call push_back:
    A (x_arg)
    A (A &)
    ~A ()
    ~A ()
     */
    

    Reference

    History

    • 20190422: created.

    Copyright

  • 相关阅读:
    Effective C++:条款14:在中小企业资源管理copying表现
    Linux在iptables教程基本应用防火墙
    C++内存分配和拷贝构造函数写研究
    Codeforces 479E Riding in a Lift(dp)
    Swift
    Swift
    Swift
    Swift
    Swift
    Swift
  • 原文地址:https://www.cnblogs.com/kezunlin/p/11909550.html
Copyright © 2011-2022 走看看