0

Maybe this question is a little bit theoretic, but I wonder what are the the design incentives behind defining std::minmax like this

template <class T>
pair<T,T> minmax (initializer_list<T> il);

Which means ,IMO, the passed object, li will be copied and each of its members must also be copy-constructible.

While, std::min_element (or for this matter std::max_element) is more "efficient" in the sense only the container iterators are being passed (no need to actually copy the entire container)

template <class ForwardIterator>
ForwardIterator min_element (ForwardIterator first, ForwardIterator last);

EDIT - based on Joachim Pileborg comment, initializer_list<T> objects are not being copied, so I'm pinpointing my question - why std::minmax is constrained to such objects and not to arbitrary containers (which have "non-const" nature, so to speak)

4

1 回答 1

5

对于您更新的问题:minmax也可以使用一对迭代器的一般情况,它被称为minmax_element. 因此,minmax它本身只是一种方便的简写,可以编写如下紧凑的东西:

// define a, b, c here
int min, max;
std::tie(min, max) = std::minmax({a, b, c});

...而不是这样写:

// define a, b, c here
int min, max;
auto list = {a, b, c};
auto result = std::minmax_element(list.begin(), list.end());
min = *result.first;
max = *result.second;
于 2014-12-16T12:41:17.857 回答