最終更新日時(UTC):
が更新

履歴 編集

function template
<forward_list>

std::forward_list::emplace_front(C++11)

template <class... Args>
void emplace_front(Args&&... args);         // C++14 まで

template <class... Args>
reference emplace_front(Args&&... args);    // C++17 から

概要

直接構築で新たな要素を先頭に追加する。

この関数の引数 args... は、要素型 value_type のコンストラクタ引数である。当関数の内部で要素型 value_type のコンストラクタを呼び出し、追加する要素を構築する。

要件

  • value_type は、コンテナに対して args から直接構築可能であること

戻り値

  • C++14 まで:なし
  • C++17 から:構築した要素への参照

計算量

定数時間

備考

  • この関数の呼び出し後、全てのイテレータは無効化されない。
  • 操作中に例外が発生した場合、副作用は発生しない。

#include <iostream>
#include <forward_list>
#include <utility>
#include <string>
#include <algorithm>

int main()
{
  std::forward_list<std::pair<int, std::string>> ls;

  ls.emplace_front(1, std::string("world"));
  ls.push_front(std::make_pair(3, std::string("hello")));

  std::for_each(ls.begin(), ls.end(), [](decltype(ls)::const_reference x) {
    std::cout << x.first << ',' << x.second << std::endl;
  });
}

出力

3,hello
1,world

バージョン

言語

  • C++11

処理系

  • Clang: ??
  • GCC: 4.7.0
  • ICC: ??
  • Visual C++: 2010, 2012, 2013, 2015, 2017
    • 2010は、可変引数テンプレートに対応していないため、argsに1つしか実引数を渡せない。
    • 2012は、可変引数テンプレートに対応していないため、不完全な実装である。

関連項目

名前 説明
push_front 先頭に要素を追加する
emplace_after 任意の位置への直接構築による要素挿入
insert_after 任意の位置への要素挿入

参照