C ++填充一个std ::地图创建不必要的对象

问题描述 投票:1回答:1

我有一个在默认的构造函数,析构函数和另一个构造一些独特的字符串打印的动画:

class Animation{
public:
   int x;
   Animation(int x) {
        std::cout << "+ animation\n";
    }

    Animation() {
        std::cout << "+ animation\n";
    }

    ~Animation() {
    std::cout << "- animation\n";
    }
}

我想填充一个std ::地图这样的对象,在std ::地图的定义是这样的:

std::map<int, Animation> animations;

当我尝试填充图,我做这种方式

void createAnimations(){
    animations[0] = Animation(10);
    animations[1] = Animation(10);
    animations[2] = Animation(10);
    animations[3] = Animation(10);
    animations[4] = Animation(10);
}

当我运行该程序,打印此

+ *animation
+ animation
- animation
+ *animation
+ animation
- animation
+ *animation
+ animation
- animation
+ *animation
+ animation
- animation
+ *animation
+ animation
- animation

为什么创建和销毁这些额外的对象?

c++ stdmap
1个回答
5
投票

使用std::map的括号运算符或std::unordered_map导致创建(使用其默认的构造函数)的分配尝试之前的条目。

它可能不如想想这样的说法:

animations[0] //operator[] invoked; default-constructed object created
= //Assignment operator
Animation(10); //Animation object constructed using Animation(int) constructor.
               //Was created as an X-value, will be move-assigned into the default-constructed object

如果你要插入地图,而它的默认构造函数被调用,你需要使用insertemplace

//May invoke move-constructor, may be elided, depending on how aggressively your compiler optimizes
animations.insert(std::make_pair(0, Animation(10));
animations.insert(std::make_pair(1, Animation(10));
animations.insert(std::make_pair(2, Animation(10));
animations.insert(std::make_pair(3, Animation(10));
animations.insert(std::make_pair(4, Animation(10));

//Will construct in-place, guaranteeing only one creation of the object
//Necessary if the object cannot be move or copy constructed/assigned
animations.emplace(0, 10);
animations.emplace(1, 10);
animations.emplace(2, 10);
animations.emplace(3, 10);
animations.emplace(4, 10);
© www.soinside.com 2019 - 2024. All rights reserved.