std::array<T,N>:: swap
From cppreference.net
|
void
swap
(
array
&
other
)
noexcept
(
/* see below */
)
;
|
(C++11以降)
(C++20以降 constexpr) |
|
コンテナの内容を other の内容と交換します。イテレータと参照が他のコンテナに関連付けられることはありません。
目次 |
パラメータ
| other | - | 内容を交換するコンテナ |
戻り値
(なし)
例外
|
noexcept
noexcept指定:
noexcept
(
noexcept
(
swap
(
std::
declval
<
T
&
>
(
)
,
std::
declval
<
T
&
>
(
)
)
)
)
上記の式において、識別子
|
(C++17まで) |
|
noexcept
noexcept指定:
noexcept
(
std::
is_nothrow_swappable_v
<
T
>
)
|
(C++17以降) |
noexcept
仕様:
noexcept
計算量
コンテナのサイズに対して線形。
例
このコードを実行
#include <array> #include <iostream> template<class Os, class V> Os& operator<<(Os& os, const V& v) { os << '{'; for (auto i : v) os << ' ' << i; return os << " } "; } int main() { std::array<int, 3> a1{1, 2, 3}, a2{4, 5, 6}; auto it1 = a1.begin(); auto it2 = a2.begin(); int& ref1 = a1[1]; int& ref2 = a2[1]; std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n'; a1.swap(a2); std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n'; // スワップ後もイテレータと参照は元の配列に関連付けられたままであることに注意 // 例えば、`it1`は依然として要素a1[0]を指し、`ref1`は依然としてa1[1]を参照する }
出力:
{ 1 2 3 } { 4 5 6 } 1 4 2 5
{ 4 5 6 } { 1 2 3 } 4 1 5 2
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2456 | C++11 |
the
noexcept
specification is ill-formed
|
made to work |
関連項目
|
(C++11)
|
std::swap
アルゴリズムを特殊化する
(関数テンプレート) |