std::inplace_vector<T,N>::emplace
来自 cppreference.cn
< cpp | container | inplace vector
template< class... Args > constexpr iterator emplace( const_iterator position, Args&&... args ); |
(since C++26) | |
将新元素插入到容器中紧靠 pos 之前的位置。通常,使用 placement new 就地构造元素,位置由容器提供。参数 args... 作为 std::forward<Args>(args)... 转发给构造函数。
本节内容不完整 |
目录 |
[edit] 参数
pos | - | 迭代器,指示新元素将在其之前构造的位置 |
args | - | 转发给元素构造函数的参数 |
类型要求 | ||
-T 必须满足 MoveAssignable、 MoveInsertable 和 EmplaceConstructible 的要求。 |
[edit] 返回值
指向插入元素的迭代器。
[edit] 复杂度
与 pos 和 end() 之间的距离呈线性关系。
[edit] 异常
如果调用前 size() == capacity(),则抛出 std::bad_alloc。该函数没有效果(强异常安全保证)。
插入元素的初始化或任何 LegacyInputIterator 操作抛出的任何异常。[
0,
pos)
中的元素不会被修改。
[edit] 示例
运行此代码
#include <cassert> #include <inplace_vector> #include <new> #include <utility> int main() { using P = std::pair<int, int>; using I = std::inplace_vector<P, 3>; auto nums = I{{0, 1}, {2, 3}}; auto it = nums.emplace(nums.begin() + 1, -1, -2); assert((*it == P{-1, -2})); assert((nums == I{P{0, 1}, {-1, -2}, {2, 3}})); try { nums.emplace(nums.begin(), 1, 3); // throws: no space } catch(const std::bad_alloc& ex) { std::cout << ex.what() << '\n'; } }
可能的输出
std::bad_alloc
[edit] 参见
插入元素 (public member function) | |
在末尾就地构造元素 (public member function) |