std::vector<T,Allocator>:: emplace
|
template
<
class
...
Args
>
iterator emplace ( const_iterator pos, Args && ... args ) ; |
(自 C++11 起)
(自 C++20 起为 constexpr) |
|
在容器中直接于 pos 位置之前插入一个新元素。
该元素通过 std::allocator_traits::construct 进行构造,此方法通常使用定位 new 在容器提供的指定位置就地构造元素。但若所需位置已被现有元素占用,则插入的元素会先在另一位置构造,随后通过移动赋值操作转入所需位置。
参数 args... 将以 std:: forward < Args > ( args ) ... 的形式转发给构造函数。 args... 可能直接或间接引用容器中的某个值。
如果操作后新的
size()
大于原来的
capacity()
,则会发生重新分配,这种情况下所有迭代器(包括
end()
迭代器)和所有元素引用都会失效。否则,只有插入点之前的迭代器和引用保持有效。
目录参数
返回值指向被安置元素的迭代器。 复杂度与 pos 和 end() 之间的距离呈线性关系。 异常
若抛出的异常并非由
否则,效果是未指定的。 示例
运行此代码
#include <iostream> #include <string> #include <vector> struct A { std::string s; A(std::string str) : s(std::move(str)) { std::cout << " constructed\n"; } A(const A& o) : s(o.s) { std::cout << " copy constructed\n"; } A(A&& o) : s(std::move(o.s)) { std::cout << " move constructed\n"; } A& operator=(const A& other) { s = other.s; std::cout << " copy assigned\n"; return *this; } A& operator=(A&& other) { s = std::move(other.s); std::cout << " move assigned\n"; return *this; } }; int main() { std::vector<A> container; // 预留足够空间以避免vector重新分配内存 container.reserve(10); std::cout << "构造2次A对象:\n"; A two{"two"}; A three{"three"}; std::cout << "原位构造:\n"; container.emplace(container.end(), "one"); std::cout << "通过左值引用进行原位构造:\n"; container.emplace(container.end(), two); std::cout << "通过右值引用进行原位构造:\n"; container.emplace(container.end(), std::move(three)); std::cout << "容器内容:\n"; for (const auto& obj : container) std::cout << ' ' << obj.s; std::cout << '\n'; } 输出: 构造2次A对象: constructed constructed 原位构造: constructed 通过左值引用进行原位构造: copy constructed 通过右值引用进行原位构造: move constructed 容器内容: one two three 缺陷报告以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
参见
| ||||||||||||||||||||||||