c++ - std::minmax initializer_list<T> argument -
maybe question little bit theoretic, wonder the design incentives behind defining std::minmax
template <class t> pair<t,t> minmax (initializer_list<t> il);
which means ,imo, passed object, li
copied , each of members must copy-constructible.
while, std::min_element
(or matter std::max_element
) more "efficient" in sense container iterators being passed (no need copy entire container)
template <class forwarditerator> forwarditerator min_element (forwarditerator first, forwarditerator last);
edit - based on joachim pileborg comment, initializer_list<t>
objects not being copied, i'm pinpointing question - why std::minmax
constrained such objects , not arbitrary containers (which have "non-const" nature, speak)
for updated question: minmax
can work general case of pair of iterators, called minmax_element
. minmax
convenience shorthand able write compact things this:
// define a, b, c here int min, max; std::tie(min, max) = std::minmax({a, b, c});
...instead of writing this:
// 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;
Comments
Post a Comment