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

履歴 編集

function
<inplace_vector>

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

constexpr void swap(inplace_vector& x)
  noexcept(N == 0 || (is_nothrow_swappable_v<T> &&
                      is_nothrow_move_constructible_v<T>)); // (1) C++26

概要

他のinplace_vectorオブジェクトとデータを入れ替える。

効果

*thisxの要素を入れ替える。

例外

N == 0、もしくは要素型Tがnothrowでswap可能かつnothrowでムーブ構築可能な場合は、例外を投げない。

計算量

size()x.size()の大きいほうに対して線形時間。

std::vectorswap()が定数時間であるのとは異なり、inplace_vectorでは要素の交換が必要なため線形時間となる。

備考

swapにより全てのイテレータが無効化される。

#include <print>
#include <inplace_vector>

int main()
{
  std::inplace_vector<int, 5> v1 = {1, 2, 3};
  std::inplace_vector<int, 5> v2 = {4, 5};

  v1.swap(v2);

  std::print("v1:");
  for (int x : v1) std::print(" {}", x);
  std::println("");

  std::print("v2:");
  for (int x : v2) std::print(" {}", x);
  std::println("");
}

出力

v1: 4 5
v2: 1 2 3

バージョン

言語

  • C++26

処理系

参照