C++ valarray 庫 - swap 函式



描述

它交換 *this 和 x 的內容。

宣告

以下是 std::valarray::swap 函式的宣告。

template <class T>
   void swap (valarray<T>& x, valarray<T>& y) noexcept;

C++11

template <class T>
   void swap (valarray<T>& x, valarray<T>& y) noexcept;

引數

x,y − 這些是相同型別的另一個 valarray 物件。

返回值

異常

基本保證 − 如果對元素執行的任何操作引發異常。

資料競爭

訪問所有有效複製的元素。

示例

以下示例說明了 std::valarray::swap 函式。

#include <iostream>
#include <valarray>

int main () {
   std::valarray<int> foo {0,10,20,30};
   std::valarray<int> bar {100,200,300};

   foo.swap(bar);

   std::cout << "foo contains:";
   for (auto& x: foo) std::cout << ' ' << x;
   std::cout << '\n';

   std::cout << "bar contains:";
   for (auto& x: bar) std::cout << ' ' << x;
   std::cout << '\n';

   return 0;
}

讓我們編譯並執行以上程式,這將產生以下結果:

foo contains: 100 200 300
bar contains: 0 10 20 30
valarray.htm
廣告

© . All rights reserved.