std::valarray<T>::operator+=,-=,*=,/=,%=,&=,|=,<<=,>>=
valarray<T>& operator+=( const valarray<T>& v ); valarray<T>& operator-=( const valarray<T>& v ); |
(1) | |
valarray<T>& operator+=( const T& val ); valarray<T>& operator-=( const T& val ); |
(2) | |
對數值陣列中的每個元素應用複合賦值運算子。
目錄 |
[編輯] 引數
v | - | 另一個數值陣列 |
val | - | 一個值 |
[編輯] 返回值
*this
[編輯] 異常
可能丟擲實現定義的異常。
[編輯] 注意
只有滿足以下要求時,每個運算子才能被例項化:
- 指定的運算子可以應用於型別
T
。 - 結果值可以明確地轉換為
T
。
[編輯] 示例
#include <iostream> #include <string_view> #include <type_traits> #include <valarray> void o(std::string_view rem, auto const& v, bool nl = false) { if constexpr (std::is_scalar_v<std::decay_t<decltype(v)>>) std::cout << rem << " : " << v; else { for (std::cout << rem << " : { "; auto const e : v) std::cout << e << ' '; std::cout << '}'; } std::cout << (nl ? "\n" : "; "); } int main() { std::valarray<int> x, y; o("x", x = {1, 2, 3, 4}), o("y", y = {4, 3, 2, 1}), o("x += y", x += y, 1); o("x", x = {4, 3, 2, 1}), o("y", y = {3, 2, 1, 0}), o("x -= y", x -= y, 1); o("x", x = {1, 2, 3, 4}), o("y", y = {5, 4, 3, 2}), o("x *= y", x *= y, 1); o("x", x = {1, 3, 4, 7}), o("y", y = {1, 1, 3, 5}), o("x &= y", x &= y, 1); o("x", x = {0, 1, 2, 4}), o("y", y = {4, 3, 2, 1}), o("x <<=y", x <<=y, 1); std::cout << '\n'; o("x", x = {1, 2, 3, 4}), o("x += 5", x += 5, 1); o("x", x = {1, 2, 3, 4}), o("x *= 2", x *= 2, 1); o("x", x = {8, 6, 4, 2}), o("x /= 2", x /= 2, 1); o("x", x = {8, 4, 2, 1}), o("x >>=1", x >>=1, 1); }
輸出
x : { 1 2 3 4 }; y : { 4 3 2 1 }; x += y : { 5 5 5 5 } x : { 4 3 2 1 }; y : { 3 2 1 0 }; x -= y : { 1 1 1 1 } x : { 1 2 3 4 }; y : { 5 4 3 2 }; x *= y : { 5 8 9 8 } x : { 1 3 4 7 }; y : { 1 1 3 5 }; x &= y : { 1 1 0 5 } x : { 0 1 2 4 }; y : { 4 3 2 1 }; x <<=y : { 0 8 8 8 } x : { 1 2 3 4 }; x += 5 : { 6 7 8 9 } x : { 1 2 3 4 }; x *= 2 : { 2 4 6 8 } x : { 8 6 4 2 }; x /= 2 : { 4 3 2 1 } x : { 8 4 2 1 }; x >>=1 : { 4 2 1 0 }
[編輯] 參閱
對 valarray 的每個元素應用一元算術運算子 (public member function) | |
對兩個 valarray 的每個元素,或一個 valarray 和一個值應用二元運算子 (function template) |