Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道两者之间有什么区别。我注意到 emplace 是 c++11 添加的。那么为什么要添加呢?
Emplace 采用就地构造对象所需的参数,而 insert 采用(引用)一个对象。
struct Foo { Foo(int n, double x); }; std::vector<Foo> v; v.emplace(someIterator, 42, 3.1416); v.insert(someIterator, Foo(42, 3.1416));
insert将对象复制到向量中。
insert
emplace 在向量内部构造它们。
emplace