zoukankan      html  css  js  c++  java
  • vector emplace_back() 和push_back() 的区别

    push_back:

    函数原型为:

    void push_back(const value_type& val);
    void push_back(value_type& val);

    作用:在vector当前最后一个元素之后添加一个新元素,会调用拷贝函数或者移动构造函数。

    // vector::push_back
    #include <iostream>
    #include <vector>
    
    int main ()
    {
      std::vector<int> myvector;
      int myint;
    
      std::cout << "Please enter some integers (enter 0 to end):
    ";
    
      do {
        std::cin >> myint;
        myvector.push_back (myint);
      } while (myint);
    
      std::cout << "myvector stores " << int(myvector.size()) << " numbers.
    ";
    
      return 0;
    }

    emplace_back:

    函数原型为:

    template <class... Args>
    void emplace_back (Args&&... args);

    作用:在vector当前最后一个元素之后添加一个新元素。这个新元素是使用args作为其构造函数的参数来构造的。

    和push_back类似,但是push_back会将现有对象拷贝或移动到新的容器,emplace_back是直接构造新的对象。
    #include <vector>  
    #include <string>  
    #include <iostream>  
    
    struct President  
    {  
        std::string name;  
        std::string country;  
        int year;  
    
        President(std::string p_name, std::string p_country, int p_year)  
            : name(std::move(p_name)), country(std::move(p_country)), year(p_year)  
        {  
            std::cout << "I am being constructed.
    ";  
        }
        President(const President& other)
            : name(std::move(other.name)), country(std::move(other.country)), year(other.year)
        {
            std::cout << "I am being copy constructed.
    ";
        }
        President(President&& other)  
            : name(std::move(other.name)), country(std::move(other.country)), year(other.year)  
        {  
            std::cout << "I am being moved.
    ";  
        }  
        President& operator=(const President& other);  
    };  
    
    int main()  
    {  
        std::vector<President> elections;  
        std::cout << "emplace_back:
    ";  
        elections.emplace_back("Nelson Mandela", "South Africa", 1994); //没有类的创建  
    
        std::vector<President> reElections;  
        std::cout << "
    push_back:
    ";  
        reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936));  
    
        std::cout << "
    Contents:
    ";  
        for (President const& president: elections) {  
           std::cout << president.name << " was elected president of "  
                << president.country << " in " << president.year << ".
    ";  
        }  
        for (President const& president: reElections) {  
            std::cout << president.name << " was re-elected president of "  
                << president.country << " in " << president.year << ".
    ";  
        }
    
    }

    资料:

    https://blog.csdn.net/xiaolewennofollow/article/details/52559364

    http://www.cplusplus.com/reference/vector/vector/emplace_back/

  • 相关阅读:
    LC 216. Combination Sum III
    基本数据类型及其包装类(二)
    基本数据类型及其包装类(一)
    访问权限控制
    关于类的对象创建与初始化
    垃圾收集机制与内存分配策略
    虚拟机字节码执行引擎
    虚拟机类加载机制
    字节码文件的内部结构之谜
    数值信息的机器级存储
  • 原文地址:https://www.cnblogs.com/xiaohaigegede/p/13612823.html
Copyright © 2011-2022 走看看