Namespaces
Variants

std::inplace_vector<T,N>:: try_emplace_back

From cppreference.net
template < class ... Args >
constexpr pointer try_emplace_back ( Args && ... args ) ;
(C++26 起)

有条件地将类型为 T 的对象追加到容器末尾。

size ( ) == capacity ( ) true ,则无任何效果。否则,将以 std:: forward < Args > ( args ) ... 进行直接非列表初始化的 T 类型对象追加至容器。

不会使任何迭代器或引用失效,除非插入操作发生时会使 end() 失效。

目录

参数

args - 要转发给元素构造函数的参数
类型要求
-
T 必须能够从 std:: forward < Args > ( args ) ... 进行 就地构造 inplace_vector 中。

返回值

std:: addressof ( back ( ) ) size ( ) < capacity ( ) 时,否则为 nullptr

复杂度

常量。

异常

任何由插入元素初始化抛出的异常。若因任何原因抛出异常,此函数不产生任何效果( 强异常安全保证 )。

注释

示例

#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); // 无可用空间 => 不执行插入
    assert(c == nullptr);
    assert((v == I{1.0 + 2.0i, 3.0 + 4.0i, 5.0 + 6.0i}));
}

参见

在末尾就地构造元素
(公开成员函数)
在末尾添加元素
(公开成员函数)
在末尾添加元素范围
(公开成员函数)
尝试在末尾添加元素范围
(公开成员函数)
无条件在末尾就地构造元素
(公开成员函数)
无条件在末尾添加元素
(公开成员函数)
移除末尾元素
(公开成员函数)
创建从参数推断类型的 std::back_insert_iterator
(函数模板)