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

履歴 編集

function template
<array>

std::swap (非メンバ関数)(C++11)

namespace std {
  template <class T, size_t N>
  void swap(array<T, N>& x, array<T, N>& y)
                 noexcept(noexcept(x.swap(y))); // C++11

  template <class T, size_t N>
  constexpr void swap(array<T, N>& x, array<T, N>& y)
                 noexcept(noexcept(x.swap(y))); // C++20
}

概要

2つのarrayオブジェクトを入れ替える。

効果

x.swap(y);

戻り値

なし

例外

x.swap(y)が例外を投げない場合、この関数は決して例外を投げない。

#include <iostream>
#include <array>
#include <string>
#include <algorithm>

template <class T, std::size_t N>
void print(const std::string& name, const std::array<T, N>& ar)
{
  std::cout << name << " : {";
  std::for_each(ar.begin(), ar.end(), [](const T& x) { std::cout << x << " "; });
  std::cout << "}" << std::endl;
}

int main ()
{
  std::array<int, 3> x = {4, 5, 6};
  std::array<int, 3> y = {1, 2, 3};

  std::swap(x, y);

  print("x", x);
  print("y", y);
}

出力

x : {1 2 3 }
y : {4 5 6 }

バージョン

言語

  • C++11

処理系

参照