Namespaces
Variants

std:: swap (std::valarray)

From cppreference.net
Défini dans l'en-tête <valarray>
template < class T >
void swap ( std:: valarray < T > & lhs, std:: valarray < T > & rhs ) noexcept ;
(depuis C++11)

Spécialise l'algorithme std::swap pour std::valarray . Échange le contenu de lhs et rhs . Appelle lhs. swap ( rhs ) .

Table des matières

Paramètres

lhs, rhs - valarrays dont le contenu doit être échangé

Valeur de retour

(aucun)

Complexité

Constante.

Exemple

#include <iostream>
#include <valarray>
void print(auto rem, const std::valarray<int>& v)
{
    std::cout << rem << '{';
    for (char sep[]{0, ' ', 0}; auto elem : v)
        std::cout << sep << elem, *sep = ',';
    std::cout << "}\n";
}
int main()
{
    std::valarray x{3, 1, 4, 1, 5};
    std::valarray y{2, 7, 1, 8};
    print("Before swap:\n" "x: ", x);
    print("y: ", y);
    std::swap(x, y);
    print("After swap:\n" "x: ", x);
    print("y: ", y);
}

Sortie :

Before swap:
x: {3, 1, 4, 1, 5}
y: {2, 7, 1, 8}
After swap:
x: {2, 7, 1, 8}
y: {3, 1, 4, 1, 5}

Voir aussi

échange avec un autre valarray
(fonction membre publique)