max() function for valarray in C++


In this article we will be discussing the working, syntax and examples of valarray::max() function in C++ STL.

What is a valarray?

std::valarray is a class which is used for representing, modifying the array of values, which supports elements-wise mathematical operations.

What is valarray::max()?

std::valarray::max() function is an inbuilt function in C++ STL, which is defined in <valarray> header file. This function returns the maximum value which is in the valarray container.

If the valarray is empty then the result which is returned is unspecified.

Syntax

V_array_name.max();

Parameters

The function accepts no parameter(s) −

Return value

This function returns the maximum value of the valarray.

Example

Input

valarray<int> arr = { 1, 2, 4, 5, 8, 9 };
arr.max();

Output

9

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   valarray<int> arr = {2, 4, 6, 8, 10};
   cout<<"Largest element is = "; cout<<arr.max() << endl;
   return 0;
}

Output

Largest element is = 10

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   valarray<int> arr = {2, 4, 6, 10, 10};
   //finding out the square root of greatest number
   int product = arr.max() * arr.max();
   cout<<"Square root of greatest number is: "<<product;
   return 0;
}

Output

Square root of greatest number is: 100

Updated on: 17-Apr-2020

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements