C++ Standard Library C++ STL Library

C++ <valarray> - operator/= Function



The C++ valarray::operator/= function is used to apply compound assignment operator (divide AND assignment operator) to each element of the valarray.

Syntax

valarray<T>& operator/=( const valarray<T>& v );
valarray<T>& operator/=( const T& val );

Parameters

v Specify another valarray.
val Specify a value of type T.

Return Value

Returns the left hand side valarray object.

Time Complexity

Depends on library implementation.

Example:

In the example below, the valarray::operator/= function is used to perform divide and assignment operation on each element of the given valarray.

#include <iostream>
#include <valarray>
using namespace std;
 
int main (){
  valarray<double> varr1 {10, 20, 30, 40};
  valarray<double> varr2 {2, 3, 4, 5};

  //result of varr1 / varr2 stored in varr1 
  varr1 /= varr2;
  
  cout<<fixed;
  cout<<"varr1 contains: ";
  for(int i = 0; i < varr1.size(); i++)
    cout<<varr1[i]<<" ";

  //further dividing each elements 
  //of varr1 by 3
  varr1 /= 3;

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

The output of the above code will be:

varr1 contains: 5.000000 6.666667 7.500000 8.000000 
varr1 contains: 1.666667 2.222222 2.500000 2.666667 

❮ C++ <valarray> Library