function

std::valarray operators

<valarray>
*** valarray member functions
 ***
valarray<T> operator+() const;
valarray<T> operator-() const;
valarray<T> operator~() const;
valarray<bool> operator!() const;

valarray<T>& operator*= (const valarray<T>& rhs);
valarray<T>& operator/= (const valarray<T>& rhs);
valarray<T>& operator%= (const valarray<T>& rhs);
valarray<T>& operator+= (const valarray<T>& rhs);
valarray<T>& operator-= (const valarray<T>& rhs);
valarray<T>& operator^= (const valarray<T>& rhs);
valarray<T>& operator&= (const valarray<T>& rhs);
valarray<T>& operator|= (const valarray<T>& rhs);
valarray<T>& operator<<= (const valarray<T>& rhs);
valarray<T>& operator>>= (const valarray<T>& rhs);

valarray<T>& operator*= (const T& val);
valarray<T>& operator/= (const T& val);
valarray<T>& operator%= (const T& val);
valarray<T>& operator+= (const T& val);
valarray<T>& operator-= (const T& val);
valarray<T>& operator^= (const T& val);
valarray<T>& operator&= (const T& val);
valarray<T>& operator|= (const T& val);
valarray<T>& operator<<= (const T& val);
valarray<T>& operator>>= (const T& val);

*** global functions: ***
template <class T> valarray<T> operator* (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<T> operator* (const T& val, const valarray<T>& rhs);
template <class T> valarray<T> operator* (const valarray<T>& lhs, const T& val);

template <class T> valarray<T> operator/ (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<T> operator/ (const T& val, const valarray<T>& rhs);
template <class T> valarray<T> operator/ (const valarray<T>& lhs, const T& val);

template <class T> valarray<T> operator% (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<T> operator% (const T& val, const valarray<T>& rhs);
template <class T> valarray<T> operator% (const valarray<T>& lhs, const T& val);

template <class T> valarray<T> operator+ (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<T> operator+ (const T& val, const valarray<T>& rhs);
template <class T> valarray<T> operator+ (const valarray<T>& lhs, const T& val);

template <class T> valarray<T> operator- (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<T> operator- (const T& val, const valarray<T>& rhs);
template <class T> valarray<T> operator- (const valarray<T>& lhs, const T& val);

template <class T> valarray<T> operator^ (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<T> operator^ (const T& val, const valarray<T>& rhs);
template <class T> valarray<T> operator^ (const valarray<T>& lhs, const T& val);

template <class T> valarray<T> operator& (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<T> operator& (const T& val, const valarray<T>& rhs);
template <class T> valarray<T> operator& (const valarray<T>& lhs, const T& val);

template <class T> valarray<T> operator| (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<T> operator| (const T& val, const valarray<T>& rhs);
template <class T> valarray<T> operator| (const valarray<T>& lhs, const T& val);

template <class T> valarray<T> operator<< (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<T> operator<< (const T& val, const valarray<T>& rhs);
template <class T> valarray<T> operator<< (const valarray<T>& lhs, const T& val);

template <class T> valarray<T> operator>> (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<T> operator>> (const T& val, const valarray<T>& rhs);
template <class T> valarray<T> operator>> (const valarray<T>& lhs, const T& val);

template <class T> valarray<bool> operator&& (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<bool> operator&& (const T& val, const valarray<T>& rhs);
template <class T> valarray<bool> operator&& (const valarray<T>& lhs, const T& val);

template <class T> valarray<bool> operator|| (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<bool> operator|| (const T& val, const valarray<T>& rhs);
template <class T> valarray<bool> operator|| (const valarray<T>& lhs, const T& val);

template <class T> valarray<bool> operator== (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<bool> operator== (const T& val, const valarray<T>& rhs);
template <class T> valarray<bool> operator== (const valarray<T>& lhs, const T& val);

template <class T> valarray<bool> operator!= (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<bool> operator!= (const T& val, const valarray<T>& rhs);
template <class T> valarray<bool> operator!= (const valarray<T>& lhs, const T& val);

template <class T> valarray<bool> operator< (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<bool> operator< (const T& val, const valarray<T>& rhs);
template <class T> valarray<bool> operator< (const valarray<T>& lhs, const T& val);

template <class T> valarray<bool> operator> (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<bool> operator> (const T& val, const valarray<T>& rhs);
template <class T> valarray<bool> operator> (const valarray<T>& lhs, const T& val);

template <class T> valarray<bool> operator<= (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<bool> operator<= (const T& val, const valarray<T>& rhs);
template <class T> valarray<bool> operator<= (const valarray<T>& lhs, const T& val);

template <class T> valarray<bool> operator>= (const valarray<T>& lhs, const valarray<T>& rhs);
template <class T> valarray<bool> operator>= (const T& val, const valarray<T>& rhs);
template <class T> valarray<bool> operator>= (const valarray<T>& lhs, const T& val);
Valarray operators
Each of this functions performs their respective operation on all the elements in the valarray.

When both the left-hand side argument and the right-side one are valarrays, the operation is performed between the corresponding elements in each array (the first element in the left with the first in the right, the second with the second, and so on...).

When one of the arguments is a value, the operation is applied to all the elements in the valarray against that value.

The class also overloads operator= and operator[] (see their corresponding references for more info).

Parameters

val
Value to use either as left-hand or right-hand operand agains all the elements in the valarray.
T is the template parameter of class valarray (i.e., its elements' type).
lhs
Left-hand side valarray object.
rhs
Right-hand side valarray object.

Return value

For the unary operators, a new valarray object with the modified values.
For the compund assignment operators, the left-hand side valarray object.
For the binary operators, a new valarray object with the results of performing the operation.
For the comparison operators, an array of bool elements with the result of the comparisons.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// valarray operators example
#include <iostream>
#include <valarray>
using namespace std;

int main ()
{
  int init[]= {10,20,30,40};
                                //     foo:           bar:

  valarray<int> foo (init, 4);  //  10 20 30 40    
  valarray<int> bar (25,4);     //  10 20 30 40    25 25 25 25

  bar += foo;                   //  10 20 30 40    35 45 55 65

  foo = bar + 10;               //  45 55 65 75    35 45 55 65

  foo -= 10;                    //  35 45 55 65    35 45 55 65

  valarray<bool> comp = (foo==bar);

  if ( comp.min() == true )
	  cout << "They are equal.\n";
  else
	  cout << "They are not equal.\n";

  return 0;
}


Output:

They are equal.

See also