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.


T max() const;


No parameter is required.

Return Value

Returns the maximum value of elements of valarray.

Time Complexity

Depends on library implementation.


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: "

  return 0;

The output of the above code will be:

The maximum value is: 25

❮ C++ <valarray> Library