Namespaces
Variants

std:: swap (std::basic_string)

From cppreference.net
std::basic_string
Défini dans l'en-tête <string>
template < class CharT, class Traits, class Alloc >

void swap ( std:: basic_string < CharT, Traits, Alloc > & lhs,

std:: basic_string < CharT, Traits, Alloc > & rhs ) ;
(jusqu'à C++17)
template < class CharT, class Traits, class Alloc >

void swap ( std:: basic_string < CharT, Traits, Alloc > & lhs,

std:: basic_string < CharT, Traits, Alloc > & rhs ) noexcept ( /* voir ci-dessous */ ) ;
(depuis C++17)
(constexpr depuis C++20)

Spécialise l'algorithme std::swap pour std::basic_string . Échange le contenu de lhs et rhs .

Équivalent à lhs. swap ( rhs ) .

Table des matières

Paramètres

lhs, rhs - chaînes dont le contenu doit être échangé

Valeur de retour

(aucun)

Complexité

Constante.

Exceptions

noexcept spécification :
noexcept ( noexcept ( lhs. swap ( rhs ) ) )
(depuis C++17)

Exemple

#include <iostream>
#include <string>
int main()
{
    std::string a = "AAA";
    std::string b = "BBBB";
    std::cout << "Before swap:\n"
                 "a = " << a << "\n"
                 "b = " << b << "\n\n";
    std::swap(a, b);
    std::cout << "After swap:\n"
                 "a = " << a << "\n"
                 "b = " << b << '\n';
}

Sortie :

Before swap:
a = AAA
b = BBBB
After swap:
a = BBBB
b = AAA

Rapports de défauts

Les rapports de défauts modifiant le comportement suivants ont été appliqués rétroactivement aux normes C++ précédemment publiées.

DR Appliqué à Comportement publié Comportement corrigé
LWG 2064 C++11 la fonction non-membre swap était noexcept et incohérente avec la fonction membre swap noexcept supprimé

Voir aussi

échange le contenu
(fonction membre publique)