max element c++ code example
Example 1: c++ max of array
cout << " max element is: " << *max_element(array , array + n) << endl;
Example 2: how to use max_element in c++ with vector
int main()
{
vector<int> a = { 1, 45, 54, 71, 76, 12 };
cout << "Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
cout << "\nMax Element = "
<< *max_element(a.begin(), a.end());
return 0;
}
Example 3: max c++
#include <iostream>
#include <algorithm>
int main () {
std::cout << "max(1,2)==" << std::max(1,2) << '\n';
std::cout << "max(2,1)==" << std::max(2,1) << '\n';
std::cout << "max('a','z')==" << std::max('a','z') << '\n';
std::cout << "max(3.14,2.73)==" << std::max(3.14,2.73) << '\n';
return 0;
}
Example 4: max_element c++
int arr[] = {1,4,2,10};
int n = 4;
cout<<*max_element(arr,arr+n);