function
<valarray>

std::valarray operators

member functions
valarray operator+() const;valarray operator-() const;valarray operator~() const;valarray<bool> operator!() const;valarray& operator*= (const valarray& rhs);valarray& operator/= (const valarray& rhs);valarray& operator%= (const valarray& rhs);valarray& operator+= (const valarray& rhs);valarray& operator-= (const valarray& rhs);valarray& operator^= (const valarray& rhs);valarray& operator&= (const valarray& rhs);valarray& operator|= (const valarray& rhs);valarray& operator<<= (const valarray& rhs);valarray& operator>>= (const valarray& rhs);valarray& operator*= (const T& val);valarray& operator/= (const T& val);valarray& operator%= (const T& val);valarray& operator+= (const T& val);valarray& operator-= (const T& val);valarray& operator^= (const T& val);valarray& operator&= (const T& val);valarray& operator|= (const T& val);valarray& operator<<= (const T& val);valarray& operator>>= (const T& val);
non-member 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 valarray objects, the operation is performed between the corresponding elements in each object (the first element of the left one with the first of the right one, 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.

If the type held in the valarray does not itself support the operation, such an operation shall not be instantiated.

The class also overloads operator= and operator[].

Parameters

val
Value to use either as left-hand or right-hand operand agains all the elements in the valarray.
T is the template argument of valarray (the value 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 compound assignment operators, the left-hand side valarray object.
For the binary operators, a new valarray object with the results of performing the operation on each element.
For the comparison operators, a valarray of bool elements with the result of each of the individual comparisons.

The operators returning a valarray by value are allowed to return an object of a different type instead. Such a type is required to be implicitly convertible to valarray and be supported as argument for all functions taking valarray& arguments. This allows copy-on-write implementations.

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
// valarray operators example
#include <iostream>     // std::cout
#include <valarray>     // std::valarray

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

  std::valarray<int> foo (init, 4);  //  10 20 30 40
  std::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

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

  if ( comp.min() == true )
    std::cout << "foo and bar are equal.\n";
  else
    std::cout << "foo and bar are not equal.\n";

  return 0;
}

Output:

foo and bar are equal.


Complexity

Depends on library implementation (operations may be parallelized).

Iterator validity

No changes: Valid iterators, references and sub-arrays keep their validity.

Data races

All elements in valarray objects involved in the operation are accessed, and -if in a compound assignment- also modified.

Exception safety

If any operation performed on the elements throws an exception, it causes undefined behavior.
If a function fails to allocate storage it needs, it may throw an exception (such as bad_alloc), although this is not mandated.

See also