std::deque<T,Allocator>:: emplace
|
template
<
class
...
Args
>
iterator emplace ( const_iterator pos, Args && ... args ) ; |
(C++11以降)
(constexpr C++26以降) |
|
コンテナに新しい要素を pos の直前に直接挿入します。
要素は std::allocator_traits::construct を通じて構築されます。これは通常、配置 new を使用して、コンテナが提供する位置で要素をその場で構築します。ただし、必要な位置が既存の要素によって占有されている場合、挿入される要素は最初に別の場所で構築され、その後必要な位置にムーブ代入されます。
引数 args... はコンストラクタに std:: forward < Args > ( args ) ... として転送されます。 args... はコンテナ内の値に直接または間接的に参照している可能性があります。
すべてのイテレータ(
end()
イテレータを含む)は無効化されます。参照も同様に無効化されますが、
pos
==
begin()
または
pos
==
end()
の場合を除きます。これらのケースでは参照は無効化されません。
目次パラメータ
戻り値配置された要素を指すイテレータ。 計算量コンテナの両端と pos との距離のうち、小さい方に対して線形時間。 例外
それ以外の場合、効果は未規定です。 例
このコードを実行
#include <iostream> #include <string> #include <deque> struct A { std::string s; A(std::string str) : s(std::move(str)) { std::cout << " constructed\n"; } A(const A& o) : s(o.s) { std::cout << " copy constructed\n"; } A(A&& o) : s(std::move(o.s)) { std::cout << " move constructed\n"; } A& operator=(const A& other) { s = other.s; std::cout << " copy assigned\n"; return *this; } A& operator=(A&& other) { s = std::move(other.s); std::cout << " move assigned\n"; return *this; } }; int main() { std::deque<A> container; std::cout << "construct 2 times A:\n"; A two{"two"}; A three{"three"}; std::cout << "emplace:\n"; container.emplace(container.end(), "one"); std::cout << "emplace with A&:\n"; container.emplace(container.end(), two); std::cout << "emplace with A&&:\n"; container.emplace(container.end(), std::move(three)); std::cout << "content:\n"; for (const auto& obj : container) std::cout << ' ' << obj.s; std::cout << '\n'; } 出力: construct 2 times A: constructed constructed emplace: constructed emplace with A&: copy constructed emplace with A&&: move constructed content: one two three 欠陥報告以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
関連項目
| ||||||||||||||||||||||||