cplusplus.com
C++ : Reference : Miscellaneous : valarray : valarray : operator=
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
valarray
classes:
gslice
gslice_array
indirect_array
mask_array
slice
slice_array
valarray
global functions:
abs
acos
asin
atan
atan2
cos
cosh
exp
log
log10
pow
sin
sinh
sqrt
tan
tanh
valarray
valarray operators
valarray::valarray
valarray::~valarray
member functions:
valarray::apply
valarray::cshift
valarray::max
valarray::min
valarray::operator=
valarray::operator[]
valarray::resize
valarray::shift
valarray::size
valarray::sum


valarray::operator=

public member function
valarray<T>& operator=(const valarray<T>& x);
valarray<T>& operator=(const T& val);
valarray<T>& operator=(const slice_array<T>& sub);
valarray<T>& operator=(const gslice_array<T>& sub);
valarray<T>& operator=(const mask_array<T>& sub);
valarray<T>& operator=(const indirect_array<T>& sub);

Assign content

Assigns content to valarray object.

The first constructor (copy constructor), assigns to each element the value of its corresponding element in x - the behavior is unspecified if sizes do not match.

The second constructor assigns to all the elements in the array the value val.

The remaining constructors assign the result of the subscriptiong operation sub to the valarray object.

Parameters

x
Another valarray. The values of its elements are copied to their corresponding elements in the object.
The behavior when lengths do not match in unspecified - depends on the library implementation.
val
All the elements are in the valarray are set to this value.
The length of the valarray is preserved, with each of its element equal to this value.
T is the template type of valarray (the elements' type).
sub
The result of a valarray subscripting operation.

Return value

*this

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// valarray assignment example
#include <iostream>
#include <valarray>
using namespace std;

int main ()
{                           //    foo:      bar:

  valarray<int> foo (4);    //  0 0 0 0
  valarray<int> bar (2,4);  //  0 0 0 0   2 2 2 2

  foo = bar;                //  2 2 2 2   2 2 2 2
  bar = 5;                  //  2 2 2 2   5 5 5 5
  foo = bar[slice (0,4,1)]; //  5 5 5 5   5 5 5 5

  cout << "foo sums " << foo.sum() << endl;

  return 0;
}


Output:

foo sums 20

See also