C++ Standard Library C++ STL Library

C++ <valarray> - operator/ Function



The C++ <valarray> operator/ function is used to apply divide operator to each element of two valarrays, or a valarray and a value.

Syntax

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

Parameters

lhs Specify left-hand side valarray.
rhs Specify right-hand side valarray.
val Specify a value of type T.

Return Value

Returns a new valarray object containing elements as a result of performing operation on each element.

Time Complexity

Depends on library implementation.

Example:

The example below shows the usage of operator/ function.

#include <iostream>
#include <valarray>
using namespace std;
 
int main (){
  valarray<double> va1 {10, 20, 30, 40};
  valarray<double> va2 {6, 7, 8, 9};
  valarray<double> va3, va4;

  //result of va1 / 2 is stored in va3 
  va3 = va1 / 2.0;

  cout<<fixed;
  cout<<"va3 contains: ";
  for(int i = 0; i < va3.size(); i++)
    cout<<va3[i]<<" ";

  //result of va1 / va2 is stored in va4 
  va4 = va1 / va2;

  cout<<"\nva4 contains: ";
  for(int i = 0; i < va4.size(); i++)
    cout<<va4[i]<<" ";  
    
  return 0;
}

The output of the above code will be:

va3 contains: 5.000000 10.000000 15.000000 20.000000 
va4 contains: 1.666667 2.857143 3.750000 4.444444 

❮ C++ <valarray> Library