c++ populate a std::map 创建不必要的对象

c++ populate a std::map create unnecessary objects

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

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

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

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

我想用这个对象填充 std::map,std::map 定义是这样的:

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

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

使用 std::mapstd::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);