std::inplace_vector<T,N>::try_emplace_back
來自 cppreference.com
< cpp | 容器 | inplace_vector
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)... 被 就地構造(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 (函式模板) |