Namespaces
Variants

std::swap(std::valarray)

ja.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 と交換する
(公開メンバ関数)