Home »
C++ STL
valarray max() and min() Functions in C++ with Examples
C++ valarray max() and min() Functions: Here, we will learn about the max() and min() functions, their usages, syntax and examples.
Submitted by Shivang Yadav, on May 07, 2022
The valarray class in C++ is a special container that is used for holding elements like an array and performing operations on them.
C++ STL std::valarray::max() Function
The max() function is defined in valarray class. This function is used to find the largest / maximum value contained in the valarray.
Syntax
T max() const;
// or
valarray_name.max()
Parameter(s)
The method does not require any parameter.
Return Value
The method returns a single element of the same type as the valarray. It is the maximum element of the valarray.
Example
#include <iostream>
#include <valarray>
using namespace std;
int main()
{
// Declaring valarray
valarray<int> myvalarr = { 1, 92, 13, 24, 55, 61, 7 };
// Printing the elements of valarray
cout << "The elements stored in valarray are : ";
for (int& ele : myvalarr)
cout << ele << " ";
cout << "\nThe largest value in the valarray is ";
cout << myvalarr.max();
return 0;
}
Output:
The elements stored in valarray are : 1 92 13 24 55 61 7
The largest value in the valarray is 92
C++ STL std::valarray::min() Function
The min() function is defined in valarray class. This function is used to find the smallest / minimum value contained in the valarray.
Syntax
T min() const;
// or
valarray_name.min()
Parameter(s)
The method does not require any parameter.
Return Value
The method returns a single element of the same type as the valarray. It is the smallest element of the valarray.
Example
#include <iostream>
#include <valarray>
using namespace std;
int main()
{
// Declaring valarray
valarray<int> myvalarr = { 1, 92, 13, 24, 55, 61, 7 };
// Printing the elements of valarray
cout << "The elements stored in valarray are : ";
for (int& ele : myvalarr)
cout << ele << " ";
cout << "\nThe smallest value in the valarray is ";
cout << myvalarr.min();
return 0;
}
Output:
The elements stored in valarray are : 1 92 13 24 55 61 7
The smallest value in the valarray is 1
Both the methods return the same type of value which is the same as the data type of valarray.
C++ valarray max() and min() Functions Example:
#include <iostream>
#include <valarray>
#include <typeinfo>
using namespace std;
int main()
{
// Declaring valarray
valarray<int> myvalarr = { 1, 92, 13, 24, 55, 61, 7 };
// Printing the elements of valarray
cout << "The elements stored in valarray are : ";
for (int& ele : myvalarr)
cout << ele << " ";
cout << "\nThe type of smallest value in the valarray is ";
cout << typeid(myvalarr.min()).name();
cout << "\nThe type of largest value in the valarray is ";
cout << typeid(myvalarr.max()).name();
return 0;
}
Output:
The elements stored in valarray are : 1 92 13 24 55 61 7
The type of smallest value in the valarray is i
The type of largest value in the valarray is i
i in the output represents an integer which is the data type of the value in valarray.