C++ valarray Library - Function operators


Description

Each of this functions performs their respective operation on all the elements in the valarray.

Declaration

Following is the declaration for std::valarray operators function.

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);

Parameters

  • val − It is a value to which each of the elements is initialized.

  • lhs − It is a left-hand side valarray object.

  • rhs − It is a right-hand side valarray object.

Exceptions

Basic guarantee − if any operation performed on the elements throws an exception.

Data races

All elements effectively copied are accessed.

Example

In below example explains about std::valarray::valarray function.

#include <iostream>
#include <valarray>

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

   std::valarray<int> foo (init, 4);
   std::valarray<int> bar (25,4);
   bar += foo;

   foo = bar + 10;

   foo -= 10;

   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;
}

Let us compile and run the above program, this will produce the following result −

foo and bar are equal.
valarray.htm
Advertisements