GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::array::swap - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. array
  3. array
  4. swap

最終更新日時: 2026年09月19日 21時31分51秒 (JST)
rotarymars が更新

履歴 編集

function
<array>

std::array::swap(C++11)

void swap(array& other)
         noexcept(noexcept(swap(declval<T&>(), declval<T&>()))); // C++11

constexpr void swap(array& other)
         noexcept(noexcept(swap(declval<T&>(), declval<T&>()))); // C++20

概要

自身と他のarrayオブジェクトの値を入れ替える

効果

swap_ranges(begin(), end(), other.begin())

戻り値

なし

例外

arrayクラスの要素型Tに対するswap操作が例外を投げない場合、このswap関数は決して例外を投げない。

計算量

線形時間

備考

0要素の場合(N == 0)、noexcept(true)となる。

例

#include <iostream>
#include <array>

#include <string>
#include <algorithm> // std::for_each

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 = {1, 2, 3};
  std::array<int, 3> y = {4, 5, 6};

  x.swap(y);

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

出力

x : 4 5 6 
y : 1 2 3 

バージョン

言語

  • C++11

処理系

  • Clang: 9.0.1 ✅
  • GCC: 4.7.0 ✅
  • Visual C++: 2008 (std::tr1) ✅, 2010 ✅, 2012 ✅

参照

  • P1032R1 Misc constexpr bits
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.