
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
#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
Advertisements