std::array<T,N>::swap
Von cppreference.com
| void swap( array& other ) noexcept(/* siehe unten */); |
(seit C++11) (constexpr seit C++20) |
|
Tauscht den Inhalt des Containers mit dem von other aus. Iteratoren und Referenzen werden nicht an den anderen Container gebunden.
Inhalt |
[bearbeiten] Parameter
| Sonstiges | - | Container, mit dem die Inhalte ausgetauscht werden sollen |
[bearbeiten] Rückgabewert
(keine)
[bearbeiten] Ausnahmen
|
noexcept-Spezifikation:
noexcept(noexcept(swap(std::declval<T&>(), std::declval<T&>()))) Im obigen Ausdruck wird die Identifikation |
(bis C++17) |
|
noexcept-Spezifikation:
noexcept(std::is_nothrow_swappable_v<T>) |
(seit C++17) |
noexcept-Spezifikation:
noexcept
[bearbeiten] Komplexität
Linear zur Größe des Containers.
[bearbeiten] Beispiel
Führen Sie diesen Code aus
#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'; // Note that after swap iterators and references stay associated with their original // array, e.g. `it1` still points to element a1[0], `ref1` still refers to a1[1]. }
Ausgabe
{ 1 2 3 } { 4 5 6 } 1 4 2 5
{ 4 5 6 } { 1 2 3 } 4 1 5 2[bearbeiten] Fehlerberichte
Die folgenden Verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
| DR | angewendet auf | Verhalten wie veröffentlicht | Korrigiertes Verhalten |
|---|---|---|---|
| LWG 2456 | C++11 | die noexcept Spezifikation ist fehlerhaft |
wurde behoben |
[bearbeiten] Siehe auch
| (C++11) |
spezialisiert den Algorithmus std::swap (function template) |