valarray<T>& operator=( const valarray<T>& other ); | (1) | |
valarray<T>& operator=( valarray<T>&& other ); | (2) | (since C++11) |
valarray<T>& operator=( const T& val ); | (3) | |
valarray<T>& operator=( const std::slice_array<T>& other); | (4) | |
valarray<T>& operator=( const std::gslice_array<T>& other); | (5) | |
valarray<T>& operator=( const std::mask_array<T>& other ); | (6) | |
valarray<T>& operator=( const std::indirect_array<T>& other ); | (7) | |
valarray<T>& operator=( std::initializer_list<T> il ); | (8) | (since C++11) |
Replaces the contents of the numeric array.
*this
is assigned the value of the corresponding element of other
. If the length of other
does not equal the length of *this
, the behavior is undefined (until C++11) first resizes as if by resize(other.size())
(since C++11).*this
with those of other
. The value of other
is unspecified after this operation. The complexity of this operation may be linear if T has non-trivial destructors, but is usually constant otherwise.*this
with a copy of val
.*this
with the result of a generalized subscripting operation. The behavior is undefined if the length of the argument does not equal the length of *this
or if any value on the left depends on the value on the right (e.g v=v[v>2]
).il
. Equivalent to *this = valarray(il)
.other | - | another numeric array (or a mask) to assign |
val | - | the value to initialize each element with |
il | - | initializer list to assign |
*this
.
noexcept
specification: noexcept
#include <valarray> #include <iostream> int main() { std::valarray<int> v1(3); v1 = -1; // from a scalar std::cout << "assigned from scalar: "; for(int n: v1) std::cout << n << ' '; std::cout << '\n'; v1 = {1, 2, 3, 4, 5, 6}; // from initializer list of different size std::cout << "assigned from initializer_list: "; for(int n: v1) std::cout << n << ' '; std::cout << '\n'; std::valarray<int> v2(3); v2 = v1[std::slice(0,3,2)]; // from slice array std::cout << "every 2nd element starting at pos 0: "; for(int n: v2) std::cout << n << ' '; std::cout << '\n'; v2 = v1[v1 % 2 == 0]; // from mask array std::cout << "values that are even : "; for(int n: v2) std::cout << n << ' '; std::cout << '\n'; std::valarray<std::size_t> idx = {0,1,2,4}; // index array v2.resize(4); // sizes must match when assigning from gen subscript v2 = v1[idx]; // from indirect array std::cout << "values at positions 0,1,2,4: "; for(int n: v2) std::cout << n << ' '; std::cout << '\n'; }
Output:
assigned from scalar: -1 -1 -1 assigned from initializer_list: 1 2 3 4 5 6 every 2nd element starting at pos 0: 1 3 5 values that are even : 2 4 6 values at positions 0,1,2,4: 1 2 3 5
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/numeric/valarray/operator=