std::inplace_vector<T,N>::emplace_back
來自 cppreference.com
< cpp | 容器 | inplace_vector
template< class... Args > constexpr reference emplace_back( Args&&... args ); |
(C++26 起) | |
在容器末尾新增一個新元素。通常,該元素透過placement-new 在容器提供的位置原地構造。引數args... 被轉發給建構函式,形如 std::forward<Args>(args)...。
除了 end()
(如果發生插入則會失效)之外,沒有迭代器或引用會失效。
目錄 |
[編輯] 引數
args | - | 轉發給元素建構函式的引數 |
型別要求 | ||
-T 必須滿足 EmplaceConstructible 的要求。 |
[編輯] 返回值
back()
,即對插入元素的引用。
[編輯] 複雜度
常數時間。
[編輯] 異常
- 如果在呼叫前 size() == capacity(),則丟擲 std::bad_alloc。
- 插入元素的初始化可能丟擲的任何異常。
如果因任何原因丟擲異常,這些函式沒有效果(強異常安全保證)。
[編輯] 示例
執行此程式碼
#include <inplace_vector> #include <new> #include <print> #include <string> #include <utility> int main() { std::inplace_vector<std::pair<std::string, std::string>, 2> fauna; std::string dog{"\N{DOG}"}; fauna.emplace_back("\N{CAT}", dog); fauna.emplace_back("\N{CAT}", std::move(dog)); std::println("fauna = {}", fauna); try { fauna.emplace_back("\N{BUG}", "\N{BUG}"); // throws: there is no space } catch(const std::bad_alloc& ex) { std::println("{}", ex.what()); } std::println("fauna = {}", fauna); }
可能的輸出
fauna = [("🐈", "🐕"), ("🐈", "🐕")] std::bad_alloc fauna = [("🐈", "🐕"), ("🐈", "🐕")]
[編輯] 參閱
新增一個元素範圍到結尾 (public member function) | |
新增元素到結尾 (public member function) | |
嘗試在末尾新增元素 (public member function) | |
嘗試在末尾就地構造元素 (public member function) | |
嘗試在末尾新增一系列元素 (public member function) | |
無條件地在末尾新增元素 (public member function) | |
無條件地在末尾就地構造元素 (public member function) | |
移除末元素 (public member function) | |
建立從引數推斷型別的std::back_insert_iterator (function template) |