std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: swap
|
void
swap
(
unordered_map
&
other
)
;
|
(depuis C++11)
(jusqu'à C++17) |
|
|
void
swap
(
unordered_map
&
other
)
noexcept
(
/* voir ci-dessous */
)
;
|
(depuis C++17)
(constexpr depuis C++26) |
|
Échange le contenu du conteneur avec celui de other . N'invoque aucune opération de déplacement, copie ou échange sur les éléments individuels.
Tous les itérateurs et références restent valides. L'itérateur
end()
est invalidé.
Hash
et
KeyEqual
doivent être
Swappable
, et les objets de ces types sont échangés par des appels non qualifiés à la fonction non-membre
swap
.
Si
std::
allocator_traits
<
allocator_type
>
::
propagate_on_container_swap
::
value
est
true
, alors les allocateurs sont échangés par un appel non qualifié à la fonction non-membre
swap
. Sinon, ils ne sont pas échangés (et si
get_allocator
(
)
!
=
other.
get_allocator
(
)
, le comportement est indéfini).
Table des matières |
Paramètres
| autre | - | conteneur avec lequel échanger le contenu |
Exceptions
|
Toute exception levée par l'échange des objets
|
(jusqu'en C++17) |
|
noexcept
spécification :
noexcept
(
std::
allocator_traits
<
Allocator
>
::
is_always_equal
::
value
&&
std::
is_nothrow_swappable
<
Hash
>
::
value
|
(depuis C++17) |
Complexité
Constante.
Exemple
#include <iostream> #include <string> #include <utility> #include <unordered_map> // print out a std::pair template<class Os, class U, class V> Os& operator<<(Os& os, const std::pair<U, V>& p) { return os << p.first << ':' << p.second; } // print out a container template<class Os, class Co> Os& operator<<(Os& os, const Co& co) { os << '{'; for (const auto& i : co) os << ' ' << i; return os << " }\n"; } int main() { std::unordered_map<std::string, std::string> m1{{"γ", "gamma"}, {"β", "beta"}, {"α", "alpha"}, {"γ", "gamma"}}, m2{{"ε", "epsilon"}, {"δ", "delta"}, {"ε", "epsilon"}}; const auto& ref = *(m1.begin()); const auto iter = std::next(m1.cbegin()); std::cout << "──────── before swap ────────\n" << "m1: " << m1 << "m2: " << m2 << "ref: " << ref << "\niter: " << *iter << '\n'; m1.swap(m2); std::cout << "──────── after swap ────────\n" << "m1: " << m1 << "m2: " << m2 << "ref: " << ref << "\niter: " << *iter << '\n'; // Note that every iterator referring to an element in one container before // the swap refers to the same element in the other container after the swap. // Same is true for references. }
Sortie possible :
──────── before swap ────────
m1: { α:alpha β:beta γ:gamma }
m2: { δ:delta ε:epsilon }
ref: α:alpha
iter: β:beta
──────── after swap ────────
m1: { δ:delta ε:epsilon }
m2: { α:alpha β:beta γ:gamma }
ref: α:alpha
iter: β:beta
Voir aussi
|
(C++11)
|
spécialise l'algorithme
std::swap
(modèle de fonction) |