我有一个Animation,它在默认构造函数,析构函数和另一个构造函数中打印一些独特的字符串:
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
为什么要创建和销毁这些多余的对象?
答案 0 :(得分: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
如果要插入地图而不调用其默认构造函数,则需要使用insert
或emplace
:
//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);