1

Suppose I have an class that has constructor taking no arguments, and an STL container of its objects: list<Object> lst; Is there a way to insert a new object in-place?

I know something like lst.push_back(Object()); is going to work, equally fast as it would use move constructor, however it seems a little bit odd that there is no function to simply create new object at the end of the list without any arguments, while there already is emplace that could fit that place.

Could you please, provide some explanation if it isn't actually possible?

4

2 回答 2

7

嗯,emplace_back()正是这样做的。

只需使用lst.emplace_back();,该函数将在列表末尾创建一个对象。

于 2014-07-17T12:59:09.767 回答
2

您可以使用 lst.emplace_back();

于 2014-07-17T12:57:40.033 回答