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

履歴 編集

function
<inplace_vector>

std::inplace_vector::push_back(C++26)

constexpr reference push_back(const T& x); // (1) C++26
constexpr reference push_back(T&& x);      // (2) C++26

概要

新たな要素を末尾に追加する。

  • (1) : xをコピーして末尾に追加する。
  • (2) : xをムーブして末尾に追加する。

戻り値

追加された要素への参照。

例外

size() == Nの場合、std::bad_alloc例外を送出する。

計算量

定数時間

備考

  • std::vectorpush_back()voidを返すのとは異なり、inplace_vectorでは追加された要素への参照を返す。

#include <print>
#include <inplace_vector>
#include <string>

int main()
{
  std::inplace_vector<std::string, 5> iv;

  // (1) コピーで追加
  std::string s = "hello";
  iv.push_back(s);

  // (2) ムーブで追加
  iv.push_back("world");

  for (const auto& x : iv) {
    std::println("{}", x);
  }
}

出力

hello
world

バージョン

言語

  • C++26

処理系

参照