std::inplace_vector<T,N>::emplace
template< class... Args >
constexpr iterator emplace( const_iterator position, Args&&... args );
|
(自C++26起) | |
向容器中直接插入一个新元素到 pos 之前。通常,元素使用定位 new 在容器提供的位置原地构造。参数 args... 作为 std::forward<Args>(args)... 转发给构造函数。
| 此部分尚不完整 |
参数
| pos | - | 新元素将构造于其前的迭代器 |
| args | - | 要转发给元素构造函数的参数 |
| 类型要求 | ||
-
T
必须满足
MoveAssignable
、
MoveInsertable
和
EmplaceConstructible
的要求。
|
||
返回值
指向被插入元素的迭代器。
复杂度
与 pos 和 end() 之间的距离呈线性关系。
异常
若在调用前 size ( ) == capacity ( ) 则抛出 std::bad_alloc 。该函数不产生任何效果( 强异常安全保证 )。
由插入元素的初始化或任何
LegacyInputIterator
操作抛出的异常。
[
0
,
pos
)
范围内的元素不会被修改。
示例
#include <cassert>
#include <inplace_vector>
#include <iostream>
#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
参见
|
插入元素
(公开成员函数) |
|
|
在容器末尾原位构造元素
(公开成员函数) |