C++ emplace_back

article/2025/11/10 20:12:28

概述

在C++11中,在引入右值的升级后,调用push_back变的更为高效,原本需要调用构造函数构造这个临时对象,然后调用拷贝构造函数将这个临时对象放入容器中。在C++11升级后,只需要调用构造函数,然后调用移动拷贝函数。

就好比,现在手里有个面包,要把这个放到面包袋子里,原来的逻辑是,生产一个面包,然后在面包袋子里根据生产的面包再复制一个一摸一样的,在引入右值引用升级之后,现在的push_back的逻辑是,生产一个面包,然后把这个面包挪到袋子里(也就是移动拷贝)而不是再照着这个面包复制一个新的。

在此基础上,可以更快些吗?答案是可以的,在C++11中,可以调用emplace_back,emplace_back直接通过参数构造对象至容器中,不需要拷贝或者移动。如果继续使用面包的例子(这里可能不太恰当了),就好比面包生产出来就直接在袋子里了,没有拷贝,没有移动。如此会更加高效。

这是标准库vector的emplace_back方法的介绍以及一些例子:

std::vector<T,Allocator>::emplace_back - cppreference.com

使用

//以下代表功能一样,但效率不一样
vector
emplace <--> insert
emplace_back​ <--> ​push_back set
emplcace <--> insertmap
emplace <--> insert

简单的使用,以vectoremplace_back为例

#include <iostream>
#include <vector>using namespace std;struct Student {string name;int age;Student(string&& n, int a):name(std::move(n)), age(a){cout << "构造" << endl;}Student(const Student& s): name(std::move(s.name)), age(s.age){cout << "拷贝构造" << endl;;}Student(Student&& s):name(std::move(s.name)), age(s.age){cout << "移动构造" << endl;}Student& operator=(const Student& s);
};int main()
{vector<Student> classes_one;vector<Student> classes_two;cout << "emplace_back:" << endl;classes_one.emplace_back("xiaohong", 24);cout << "push_back:" << endl;classes_two.push_back(Student("xiaoming", 23));
}

执行结果

原理

push_back():先向容器尾部添加一个右值元素(临时对象),然后调用构造函数构造出这个临时对象,最后调用移动构造函数将这个临时对象放入容器中并释放这个临时对象。
注:最后调用的不是拷贝构造函数,而是移动构造函数。因为需要释放临时对象,所以通过std::move进行移动构造,可以避免不必要的拷贝操作
emplace_back():在容器尾部添加一个元素,调用构造函数原地构造,不需要触发拷贝构造和移动构造。因此比push_back()更加高效。

扩展

验证vector是以2倍增长的(g++)

#include <iostream>
#include <vector>using namespace std;struct Student {string name;int age;Student(string&& n, int a):name(std::move(n)), age(a){cout << "构造" << endl;}Student(const Student& s): name(std::move(s.name)), age(s.age){cout << "拷贝构造" << endl;;}Student(Student&& s):name(std::move(s.name)), age(s.age){cout << "移动构造" << endl;}Student& operator=(const Student& s);
};int main()
{vector<Student> classes;for(int i=1;i<100;i++) {classes.emplace_back("xiaohong", 24);cout<<i<<"--"<<classes.capacity();}
}

结果

构造
1--1构造
拷贝构造
2--2构造
拷贝构造
拷贝构造
3--4构造
4--4构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
5--8构造
6--8构造
7--8构造
8--8构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
9--16构造
10--16构造
11--16构造
12--16构造
13--16构造
14--16构造
15--16构造
16--16构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
17--32构造
18--32构造
19--32构造
20--32构造
21--32构造
22--32构造
23--32构造
24--32构造
25--32构造
26--32构造
27--32构造
28--32构造
29--32构造
30--32构造
31--32构造
32--32构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
33--64构造
34--64构造
35--64构造
36--64构造
37--64构造
38--64构造
39--64构造
40--64构造
41--64构造
42--64构造
43--64构造
44--64构造
45--64构造
46--64构造
47--64构造
48--64构造
49--64构造
50--64构造
51--64构造
52--64构造
53--64构造
54--64构造
55--64构造
56--64构造
57--64构造
58--64构造
59--64构造
60--64构造
61--64构造
62--64构造
63--64构造
64--64构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
拷贝构造
65--128构造
66--128构造
67--128构造
68--128构造
69--128构造
70--128构造
71--128构造
72--128构造
73--128构造
74--128构造
75--128构造
76--128构造
77--128构造
78--128构造
79--128构造
80--128构造
81--128构造
82--128构造
83--128构造
84--128构造
85--128构造
86--128构造
87--128构造
88--128构造
89--128构造
90--128构造
91--128构造
92--128构造
93--128构造
94--128构造
95--128构造
96--128构造
97--128构造
98--128构造

vs2019结果

construct 
1--1
construct 
copy construct
2--2
construct 
copy construct
copy construct
3--3
construct 
copy construct
copy construct
copy construct
4--4
construct 
copy construct
copy construct
copy construct
copy construct
5--6
construct 
6--6
construct 
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
7--9
construct 
8--9
construct 
9--9
construct 
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
10--13
construct 
11--13
construct 
12--13
construct 
13--13
construct 
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
14--19
construct 
15--19
construct 
16--19
construct 
17--19
construct 
18--19
construct 
19--19
construct 
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
20--28
construct 
21--28
construct 
22--28
construct 
23--28
construct 
24--28
construct 
25--28
construct 
26--28
construct 
27--28
construct 
28--28
construct 
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
29--42
construct 
30--42
construct 
31--42
construct 
32--42
construct 
33--42
construct 
34--42
construct 
35--42
construct 
36--42
construct 
37--42
construct 
38--42
construct 
39--42
construct 
40--42
construct 
41--42
construct 
42--42
construct 
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
43--63
construct 
44--63
construct 
45--63
construct 
46--63
construct 
47--63
construct 
48--63
construct 
49--63
construct 
50--63
construct 
51--63
construct 
52--63
construct 
53--63
construct 
54--63
construct 
55--63
construct 
56--63
construct 
57--63
construct 
58--63
construct 
59--63
construct 
60--63
construct 
61--63
construct 
62--63
construct 
63--63
construct 
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
64--94
construct 
65--94
construct 
66--94
construct 
67--94
construct 
68--94
construct 
69--94
construct 
70--94
construct 
71--94
construct 
72--94
construct 
73--94
construct 
74--94
construct 
75--94
construct 
76--94
construct 
77--94
construct 
78--94
construct 
79--94
construct 
80--94
construct 
81--94
construct 
82--94
construct 
83--94
construct 
84--94
construct 
85--94
construct 
86--94
construct 
87--94
construct 
88--94
construct 
89--94
construct 
90--94
construct 
91--94
construct 
92--94
construct 
93--94
construct 
94--94
construct 
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
copy construct
95--141
construct 
96--141
construct 
97--141
construct 
98--141
construct 
99--141


http://chatgpt.dhexx.cn/article/24BnFXJW.shtml

相关文章

list容器下的 emplace_front() splice() 函数

目录 emplace_front()splice()作者的坑时间复杂度注意点&#xff1a;疑惑处 emplace_front() emplace中文为安置&#xff0c;那么这个函数就是安置到什么什么前面。 void emplace_front(value_type val) ;时间复杂度&#xff1a;O(1) splice() splice译为粘接&#xff0c;作用…

C++优化之使用emplace、emplace_back

在C开发过程中&#xff0c;我们经常会用STL的各种容器&#xff0c;比如vector&#xff0c;map&#xff0c;set等。在使用这些容器的过程中&#xff0c;我们会大量用到的操作就是插入操作&#xff0c;比如vector的push_back&#xff0c;map的insert&#xff0c;set的insert。这些…

emplace_back深度剖析

一&#xff0c;emplace_back和push_back 1&#xff0c;直接插入对象&#xff1a;emplace_back和push_back无区别 ①当传递已经存在的对象时&#xff0c;是无区别的 #include <iostream> #include <vector>using namespace std;/* C11 STL 容器 push/inser…

push_back和emplace_back区别

在使用vector容器时&#xff0c;往容器里添加元素时&#xff0c;有push_back和emplace_back两种方法&#xff0c;一般用得最多得是push_back&#xff0c;下面看看这两种方法得区别&#xff1a; push_back源码&#xff0c;有重载得左值和右值&#xff0c;关于左值和右值可以查看…

C++11之emplace_back

在之前的学习中&#xff0c;了解到在STL中&#xff0c;进行插入元素的时候&#xff0c;有insert和push两种选择方式&#xff0c;而在有了右值引用和移动语义的时候&#xff0c;就提出了更高效的插入方法&#xff1a;emplace_back&#xff0c;下面来介绍一下C11新特性中的emplac…

C++的emplace

一、背景 在C开发过程中&#xff0c;我们经常会用STL的各种容器&#xff0c;比如vector&#xff0c;map&#xff0c;set等&#xff0c;这些容器极大的方便了我们的开发。在使用这些容器的过程中&#xff0c;我们会大量用到的操作就是插入操作&#xff0c;比如vector的push_bac…

C++ emplace_back用法介绍

C 11对容器的push_back, push_front, insert 增加了新的用法&#xff0c;与之对应的是emplace_back&#xff0c;emplace_front, emplace. 它们的作用是在操作容器时可以调用对应类型的构造数&#xff0c;例如下面的代码&#xff1a; #include <iostream> #include <v…

C++ STL中的 emplace

英文释义&#xff08;以前还真的很少用到这个单词&#xff0c;但是经常在键入empty()函数的时候冒出来&#xff09;&#xff1a; emplace 英 [ɪmpleɪs] 美 [ɪmpleɪs] v. 放列&#xff0c;安置&#xff0c;安放; 相对于insert、push、push_back系列先构造临时变量再复制…

stl之emplace函数的使用

c11新标准引入了三个新成员-------emplace_front,emplace和emplace_back,这些操作构造而不是拷贝元素&#xff0c;因此相比push_back等函数能更好地避免内存的拷贝与移动&#xff0c;使容器插入元素的性能得到进一步提升。这些操作分别对应push_front,insert和push_back&#x…

数字分解算法的优化

http://bbs.csdn.net/topics/90040267 以上是讨论的论坛 下面是一个算法&#xff1a; //数字为n&#xff0c;开始分解第k个数字void decompose(int n,int k){int i,j;//j用来表示数字是否分解完毕for(jn;j>1;j--){a[k]j;if(jn){for(int temp1;temp<k;temp)cout<<…

整数分解(java)

public class demo4 {public static void main(String[] args) {System.out.println("请输入一个数&#xff1a;");Scanner in new Scanner(System.in);int number in.nextInt();int result 0;do {int digit number % 10;result result * 10 digit;System.out.…

C语言程序——分解三位整数的各位数字

文章目录 前言一、分解三位整数二、程序实例1.程序代码2.运行结果3.结果分析 三、拓展应用总结 前言 程序设计中用到的整型数据和实际中的整数一样&#xff0c;也分为个位、十位和百位。 一、分解三位整数 取余运算可以得到数据的个位数。因此对于实际分解过程进行模拟&#…

Raptor-数字分解

1. 问题描述 之前写过一些 Raptor 的程序&#xff0c;里面经常会直接或间接遇到数字分解的要求。比如一个数 num 1234567&#xff0c;把这个数字逆序输出&#xff1b;判断一个数是否为水仙花数&#xff0c;1531^3 5^3 3^3&#xff0c;需要提取各位数字&#xff1b;或者 求一…

C++数字分解

数字分解 【问题描述】 任何一个大于1的自然数n,总可以拆分成若干个小于n的自然数之和&#xff0c;当n等于5时有6种拆分方法&#xff1a; 511111 51112 5113 5122 514 523输入&#xff1a;一行包含一个正整数n&#xff08;1<n<10&#xff09;。 输出&#xff1a;先将拆分…

整数分解方法

题目大意&#xff1a;给定一个整数n&#xff0c;找到k个数&#xff0c;使得其和等于n。 如&#xff1a; 41111 4112; 413; 422&#xff1b; 44&#xff1b; 求其分解的所有可能&#xff0c;并输出分解表达式。 解题思路&#xff1a;要拆分整数n&#xff0c;肯定先要找到一个…

史上最全的整数分解方法(包含经典的分苹果问题)

【华为OD机试真题 2022&2023】真题目录 点这里 【华为OD机试真题】信号发射和接收 &试读& 点这里 【华为OD机试真题】租车骑绿道 &试读& 点这里 整数分解方法总结 一、加法分解&#xff1a; 题目描述&#xff1a; 给定一个正整数&#xff0c;我们可以…

js输出1-100之间所有的质数并求总个数

代码如下&#xff1a; <!DOCTYPE html> <html lang"zh"> <head><meta charset"UTF-8" /><meta name"viewport" content"widthdevice-width, initial-scale1.0" /><meta http-equiv"X-UA-Compa…

JavaScript输出杨辉三角形

JavaScript输出杨辉三角形 杨辉三角形的特点和规律代码如下&#xff1a;结果 杨辉三角形的特点和规律 起始行为第0行&#xff0c;第N行为N1个数从 N > 2行开始&#xff0c;每一行的数值&#xff08;不包含两边的数值&#xff09;都是上一行两个数字的相加。当 J1 或 JN1时&…

Vscode中JS输出乱码问题的解决

一直很好用vscode突然不好用了&#xff0c;原来输出正常的JS代码在输出中都是乱码。于是上网查答案&#xff0c;试了很多奇奇怪怪的答案&#xff0c;然而没有一款能够解决我这个问题。仔细琢磨&#xff0c;既然以前好用&#xff0c;现在不好用&#xff0c;应该是某个电脑操作“…

js输出九九乘法表

js输出九九乘法表 控制台输出 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"viewport" content"wid…