emplace_back() 未按预期运行

emplace_back() does not behave as expected(emplace_back() 未按预期运行)

本文介绍了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() 未按预期运行

基础教程推荐