std::swap(std::valarray)
來自 cppreference.com
定義於標頭檔案 <valarray> |
||
template< class T > void swap( std::valarray<T>& lhs, std::valarray<T>& rhs ) noexcept; |
(C++11 起) | |
為 std::valarray 特化 std::swap 演算法。交換 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 交換 (public member function) |