因为可能发生再分配,emplace_back 对vector 要求元素类型可移动插入 (MoveInsertable) 。 示例下列代码用 emplace_back 追加President 类型的对象到 std::vector。它演示 emplace_back 如何转发参数给 President 的构造函数,并展示如何用 emplace_back 避免用 push_back 时的额外复制或移动操作。 运行此代码 #include...
template< class... Args > reference emplace_back( Args&&... args ); (C++17 起) (C++26 起为 constexpr) 添加新元素到容器尾。元素通过 std::allocator_traits::construct 构造,通常用布置 new 在容器所提供的位置原位构造元素。实参 args... 以std::forward<Args>(args)... 转发到构造函数。
deque::push_back deque::emplace_back (C++11) deque::append_range (C++23) deque::pop_back deque::resize deque::swap Non-member functions operator==operator!=operator<operator>operator<=operator>=operator<=> (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20) ...
emplace_back: I am being constructed. push_back: I am being constructed. I am being moved. Contents: Nelson Mandela was elected president of South Africa in 1994. Franklin Delano Roosevelt was re-elected president of the USA in 1936. ...
cppreference.com Build your website for just $3.88/mth. More value and performance with Namecheap.ads via Carbon Planned Maintenance The site will be in a temporary read-only mode in the next few weeks to facilitate some long-overdue software updates. We apologize for any inconvenience this ...
emplace_back (C++11) construit des éléments en place à la fin (fonction membre publique) [edit] pop_back supprime le dernier élément Original: removes the last element The text has been machine-translated via Google Translate. You can help to correct and verify the translation. ...
emplace_back (C++11) baut Elemente-Platz am Ende Original: constructs elements in-place at the end The text has been machine-translated viaGoogle Translate. You can help to correct and verify the translation. Clickherefor instructions.
std::vector<T,Allocator>::emplace_back From cppreference.com <cpp |container |vector std::vector Member types Member functions vector::vector vector::~vector vector::operator= vector::assign vector::assign_range (C++23) vector::get_allocator ...
constexpr reference unchecked_emplace_back( Args&&... args ); (since C++26) Appends a new element to the end of the container. Typically, the element is constructed using placement-new to construct the element in-place at the location provided by the container. The arguments args... are ...
与Container::emplace_back 的复杂度相同。 示例运行此代码 #include <iostream> #include <stack> struct S { int id; S(int i, double d, std::string s) : id{i} { std::cout << "S::S(" << i << ", " << d << ", \"" << s << "\");\n"; } }; int main() { std:...