Why is std :: minmax_element needed?

Why do we need a combo version std::min_element, and std::max_elementfor std::minmax_element? Is it just to keep comparisons or other benefits behind it?

+4
source share
2 answers

std::min_elementand std::max_elementyou must go through the entire container before they are returned. If you use std::min_element, then std::max_elementthese are two workarounds, whereas if you use std::minmax_element, you only need one.

So, it is “easy” to save comparisons, but I think that halving the amount of work you need to get the necessary data without losing clarity is very useful.

+10
source

.

:

std::make_pair(std::min_element(), std::max_element()) , , , std::max_element .

- , std::minmax_element , std::min_element, std::max_element .

+3

Source: https://habr.com/ru/post/1618259/


All Articles