3
votes

I am trying to find out the inbuilt mechanism of how does the max or min function work in c++ in STL for finding the maximum or minimum element in an array of elements.

1

1 Answers

2
votes

In STL, it's min_element and max_element respectively, and it accepts Iterator as the parameter type rather than the collection itself. It is not called min and max in order to prevent name collision with the CRT min and max functions/macros.

It is well-documented here: http://www.cplusplus.com/reference/algorithm/min_element/

Note that Iterator is a stand-in for a variety of types, including raw pointers.

Example:

int myints[] = {3,7,2,5,6,4,9};
std::cout << "The smallest element is " << *std::min_element( myints, myints+7 ) << '\n';