c++ find max code example
Example 1: max element in array c++ stl
*max_element (first_index, last_index);
ex:- for an array arr of size n
*max_element(arr, arr + n);
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;
}