emplace_back() 和 push_back 的区别

emplace_back() 和 push_back 的区别emplace back

emplace_back

函数原型:

template 
  
    void emplace_back (Args&& 
   ... args); 
  
#include 
    
    #include 
    
    #include 
    
    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.\n"; } President(const President& other) : name(std::move(other.name)), country(std::move(other.country)), year(other.year) { std::cout << "I am being copy constructed.\n"; } President(President&& other) : name(std::move(other.name)), country(std::move(other.country)), year(other.year) { std::cout << "I am being moved.\n"; } President& operator=(const President& other); }; int main() { std::vector 
    elections; std::cout << "emplace_back:\n"; elections.emplace_back("Nelson Mandela", "South Africa", 1994); //没有类的创建  std::vector 
    reElections; std::cout << "\npush_back:\n"; reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936)); std::cout << "\nContents:\n"; for (President const& president: elections) { std::cout << president.name << " was elected president of " << president.country << " in " << president.year << ".\n"; } for (President const& president: reElections) { std::cout << president.name << " was re-elected president of " << president.country << " in " << president.year << ".\n"; } } 

输出

emplace_back: I am being constructed. push_back: I am being constructed. I am being moved. Contents: Nelson Mandela was elected president of South Africa in 1994.
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/210780.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午11:56
下一篇 2026年3月18日 下午11:56


相关推荐

  • linux shell将字符串分割数组

    linux shell将字符串分割数组经常用将字符串分割为数组的需求。在shell中常用的方式为以下两种#!/bin/bashfunctionsplit_1(){x=”a,b,c,d”OLD_IFS=”$IFS”IFS=”,”array=($x)IFS=”$OLD_IFS”foreachin${array[*]}doecho

    2022年4月28日
    71
  • 智能交通焕发勃勃生机,未来会呈现哪些巨变?[通俗易懂]

    智能交通焕发勃勃生机,未来会呈现哪些巨变?

    2022年1月22日
    66
  • vue3 axios安装及使用

    vue3 axios安装及使用vue3axios 安装及使用安装使用 npm 安装 npminstallax 封装 axios Author Axios 封装 Date 2020 12 0810 39 03 LastEditTime 2021 10 2211 34 08 LastEditors PleasesetLas Description InUserSettin FilePath blogs s src api

    2026年3月18日
    2
  • 【撸网站第一天】开篇

    【撸网站第一天】开篇今天准备撸一个网站,主要撸一下他的技术点,包括:前端、结构、后端、压力测试等等。现介绍一下这个网站:http://xingship.com测试下来,这个网站貌似只能在手机上使用,我在pc上显示的效果并不是很好,不知道站长是否可以兼容一下pc端。如下效果:很丑,但是当我跑到手机上来看的时候,还像那么回事。网站呢,主要是电影短视频的介绍。开始我以为这个是网站的布局是这一个标准的网格布局,但是其实他是个瀑布模式。第二篇,我们来详细介绍这个,好啦,今天由于时间关系,就现记录到这..

    2022年8月31日
    5
  • grok-2-vision-latest 模型解析与应用场景汇总

    grok-2-vision-latest 模型解析与应用场景汇总

    2026年3月12日
    3
  • Java面试宝典4.0版

    Java面试宝典4.0版JAVA面试宝典V4.0版本基础1.简述JDK跟JRE的区别Jdk是java开发人员在开发过程使用的软件开发包,他提供了java的开发环境和运行环境JRE是JavaRuntimeEnviroment是指Java的运行环境如果你只想跑java程序,只要安装jre就够了,如果要从事开发就得安装jdk2.简述path跟classpat…

    2026年1月30日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号