emplace_back() does not behave as expected(emplace_back() 未按预期运行)
问题描述
我编写了一个简单的程序来尝试在标准库容器中就地创建对象.这是我写的:
I wrote a simple program to play around with in-place creation of objects inside standard library containers. This is what I wrote:
#include <vector>
#include <iostream>
class AB
{
public:
explicit AB(int n);
AB(const AB& other) = delete;
AB(AB&& other);
AB& operator=(const AB& other) = delete;
AB& operator=(AB&& other) = default;
private:
int i;
};
AB::AB(int n): i( n )
{
std::cout << "Object created." << std::endl;
};
AB::AB(AB&& other): i( std::move(other.i) )
{
std::cout << "Object moved." << std::endl;
};
int main()
{
std::vector< AB > v;
v.emplace_back(1);
v.emplace_back(2);
v.emplace_back(3);
};
我用 g++(4.8.2 版)编译它.运行输出后,我得到:
I compiled it with g++ (version 4.8.2). After running the output, I got:
Object created.
Object created.
Object moved.
Object created.
Object moved.
Object moved.
但我期待的是这样的:
Object created.
Object created.
Object created.
我认为安置的全部意义在于摆脱移动构造函数调用.AB类有什么要求没有满足吗?
I thought the whole point of emplacement was to get rid of the movement constructor calls. Are there any requirements in class AB that are not met?
感谢您的帮助.
推荐答案
问题是当您添加更多元素时,您的矢量正在调整大小,从而导致额外的移动.如果一开始就预留了足够的容量,就会得到预期的结果:
The problem is that your vector is being resized as you add more elements, resulting in extra moves. If you reserve enough capacity at the start, you get the expected result:
std::vector< AB > v;
v.reserve(3);
v.emplace_back(1);
v.emplace_back(2);
v.emplace_back(3);
给予
Object created.
Object created.
Object created.
在 gcc 4.8.2 上.请注意,您可以通过查看 v.capacity()
来跟踪原始代码中向量的增长.
On gcc 4.8.2. Note that you can track the vector's growth in your original code by looking at v.capacity()
.
这篇关于emplace_back() 未按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:emplace_back() 未按预期运行


基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01