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) | |
Applies compound assignment operators to each element in the numeric array.
Contents |
[edit] Parameters
v | - | another numeric array |
val | - | a value |
[edit] Return value
*this
[edit] Exceptions
May throw implementation-defined exceptions.
[edit] Notes
Each of the operators can only be instantiated if the following requirements are met:
- The indicated operator can be applied to type
T
. - The result value can be unambiguously converted to
T
.
[edit] Example
#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); }
Output:
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 }
[edit] See also
applies a unary arithmetic operator to each element of the valarray (public member function) | |
applies binary operators to each element of two valarrays, or a valarray and a value (function template) |