std::inplace_vector<T,N>:: emplace
|
template
<
class
...
Args
>
constexpr iterator emplace ( const_iterator position, Args && ... args ) ; |
(C++26 起) | |
在容器中直接于 pos 位置之前插入新元素。通常,该元素通过 placement 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 <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); // 抛出异常:空间不足 } catch(const std::bad_alloc& ex) { std::cout << ex.what() << '\n'; } }
可能的输出:
std::bad_alloc
参见
|
插入元素
(公开成员函数) |
|
|
在容器末尾原位构造元素
(公开成员函数) |