Namespaces
Variants

std:: swap (std::valarray)

From cppreference.net
ヘッダーで定義 <valarray>
template < class T >
void swap ( std:: valarray < T > & lhs, std:: valarray < T > & rhs ) noexcept ;
(C++11以降)

std::swap アルゴリズムを std::valarray に対して特殊化します。 lhs rhs の内容を交換します。 lhs. swap ( rhs ) を呼び出します。

目次

パラメータ

lhs, rhs - 内容を交換するvalarray

戻り値

(なし)

計算量

定数。

#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);
}

出力:

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}

関連項目

別の valarray と交換する
(公開メンバ関数)