std::inplace_vector<T,N>::try_emplace_back
来自 cppreference.cn
< cpp | container | inplace vector
template< class... Args > constexpr pointer try_emplace_back( Args&&... args ); |
(自 C++26 起) | |
有条件地将 T
类型的对象追加到容器的末尾。
如果 size() == capacity() 为 true,则不产生任何影响。 否则,使用 std::forward<Args>(args)... 对象 T
直接非列表初始化追加。
除了 end()
之外,没有迭代器或引用失效,如果发生插入,则 end()
会失效。
内容 |
[编辑] 参数
args | - | 转发给元素构造函数的参数 |
类型要求 | ||
-T 必须可从 std::forward<Args>(args)... 就地构造 (EmplaceConstructible) 到 inplace_vector 中。 |
[编辑] 返回值
如果 size() < capacity(),则为 std::addressof(back()),否则为 nullptr。
[编辑] 复杂度
常数。
[编辑] 异常
插入元素初始化抛出的任何异常。 如果由于任何原因抛出异常,则此函数不起作用(强异常安全保证)。
[编辑] 注释
本节尚不完整 原因:解释此 API 的目的。 |
[编辑] 示例
运行此代码
#include <cassert> #include <complex> #include <inplace_vector> int main() { using namespace std::complex_literals; using C = std::complex<double>; using I = std::inplace_vector<C, 3>; auto v = I{1.0 + 2.0i, 3.0 + 4.0i}; C* c = v.try_emplace_back(5.0, 6.0); assert(*c == 5.0 + 6.0i); assert((v == I{1.0 + 2.0i, 3.0 + 4.0i, 5.0 + 6.0i})); c = v.try_emplace_back(7.0, 8.0); // no space => no insertion assert(c == nullptr); assert((v == I{1.0 + 2.0i, 3.0 + 4.0i, 5.0 + 6.0i})); }
[编辑] 参见
在末尾就地构造元素 (公有成员函数) | |
在末尾添加一个元素 (公有成员函数) | |
在末尾添加一系列元素 (公有成员函数) | |
尝试在末尾添加一系列元素 (公有成员函数) | |
无条件地在末尾就地构造元素 (公有成员函数) | |
无条件地在末尾添加一个元素 (公有成员函数) | |
移除最后一个元素 (公有成员函数) | |
创建从参数推断类型的 std::back_insert_iterator (函数模板) |