std::inplace_vector<T,N>:: swap
From cppreference.net
<
cpp
|
container
|
inplace vector
|
constexpr
void
swap
(
inplace_vector
&
other
)
noexcept
(
/* voir ci-dessous */
)
;
|
(depuis C++26) | |
Échange le contenu du conteneur avec celui de other . Ne provoque pas l'association des itérateurs et références avec l'autre conteneur.
Table des matières |
Paramètres
| autre | - | conteneur avec lequel échanger le contenu |
Valeur de retour
(aucun)
Exceptions
noexcept
spécification :
noexcept
(
N
==
0
||
( std:: is_nothrow_swappable_v < T > && std:: is_nothrow_move_constructible_v < T > ) )
( std:: is_nothrow_swappable_v < T > && std:: is_nothrow_move_constructible_v < T > ) )
Complexité
Linéaire en fonction de la taille du conteneur.
Exemple
Exécuter ce code
#include <inplace_vector> #include <print> int main() { std::inplace_vector<int, 3> a1{1, 2, 3}, a2{4, 5, 6}; auto i1 = a1.begin(); auto i2 = a2.begin(); int& r1 = a1[1]; int& r2 = a2[1]; auto print_them_all = [&](auto rem) { std::println("{}a1 = {}, a2 = {}, *i1 = {}, *i2 = {}, r1 = {}, r2 = {}", rem, a1, a2, *i1, *i2, r1, r2); }; print_them_all("Before swap:\n"); a1.swap(a2); print_them_all("After swap:\n"); // Note that after swap() iterators and references stay associated with their // original sites, e.g., i1 points to element a1[0], r1 refers to a1[1]. }
Sortie :
Before swap: a1 = [1, 2, 3], a2 = [4, 5, 6], *i1 = 1, *i2 = 4, r1 = 2, r2 = 5 After swap: a1 = [4, 5, 6], a2 = [1, 2, 3], *i1 = 4, *i2 = 1, r1 = 5, r2 = 2
Voir aussi
|
(C++26)
|
spécialise l'algorithme
std::swap
(modèle de fonction) |