C++ algorithm (max_element, min_element)

 


std::max_element

Defined in header <algorithm>
(1)
template< class ForwardIt >
ForwardIt max_element( ForwardIt first, ForwardIt last );
(until C++17)
template< class ForwardIt >
constexpr ForwardIt max_element( ForwardIt first, ForwardIt last );
(since C++17)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt max_element( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last );
(2)(since C++17)
(3)
template< class ForwardIt, class Compare >
ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp );
(until C++17)
template< class ForwardIt, class Compare >
constexpr ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp );
(since C++17)
template< class ExecutionPolicy, class ForwardIt, class Compare >
ForwardIt max_element( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Compare comp );
(4)(since C++17)

Finds the greatest element in the range [first, last).


std::min_element

Defined in header <algorithm>
(1)
template< class ForwardIt >
ForwardIt min_element( ForwardIt first, ForwardIt last );
(until C++17)
template< class ForwardIt >
constexpr ForwardIt min_element( ForwardIt first, ForwardIt last );
(since C++17)
template< class ExecutionPolicy, class ForwardIt >

ForwardIt min_element( ExecutionPolicy&& policy,

                       ForwardIt first, ForwardIt last );
(2)(since C++17)
(3)
template< class ForwardIt, class Compare >
ForwardIt min_element( ForwardIt first, ForwardIt last, Compare comp );
(until C++17)
template< class ForwardIt, class Compare >
constexpr ForwardIt min_element( ForwardIt first, ForwardIt last, Compare comp );
(since C++17)
template< class ExecutionPolicy, class ForwardIt, class Compare >

ForwardIt min_element( ExecutionPolicy&& policy,

                       ForwardIt first, ForwardIt last, Compare comp );
(4)(since C++17)

Finds the smallest element in the range [first, last).




Preview:

#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>

static bool abs_compare(int a, int b)
{
	return (std::abs(a) < std::abs(b));
}

int main()
{
	std::vector<int> v{ 2,-10000,3,5,6,4,1,9,2,3,100 };
	std::vector<int>::iterator result;

	result = std::max_element(v.begin(), v.end());
	std::cout << "max element at: " << std::distance(v.begin(), result) << ", val: " << *result << std::endl;
	auto result2 = std::max_element(v.begin(), v.end(), abs_compare);
	std::cout << "max element(abs) at: " << std::distance(v.begin(), result2) << ", val: " << *result2 << std::endl;

	result = std::min_element(v.begin(), v.end());
	std::cout << "min element at: " << std::distance(v.begin(), result) << ", val: " << *result << std::endl;
	result2 = std::min_element(v.begin(), v.end(), abs_compare);
	std::cout << "min element(abs) at: " << std::distance(v.begin(), result2) << ", val: " << *result2 << std::endl;

	return 0;
}


output:

max element at: 10, val: 100
max element(abs) at: 1, val: -10000
min element at: 1, val: -10000
min element(abs) at: 6, val: 1







댓글