C++ Standard Library C++ STL Library

C++ <valarray> - max() Function



The C++ valarray::max function returns the maximum value of the elements of a valarray. It uses operator< to compare the elements. If the size of the valarray is zero, the behavior is undefined.

Syntax

T max() const;

Parameters

No parameter is required.

Return Value

Returns the maximum value of elements of valarray.

Time Complexity

Depends on library implementation.

Example:

In the example below, the valarray::max function is used to find out the maximum value of the elements of given valarray.

#include <iostream>
#include <valarray>
using namespace std;
 
int main (){
  valarray<int> varr = {10, 20, 25, 5, 15};

  //finding the maximum value of 
  //elements of the valarray
  cout<<"The maximum value is: "
      <<varr.max();  

  return 0;
}

The output of the above code will be:

The maximum value is: 25

❮ C++ <valarray> Library