Why does std::min only support initializer_list?

后端 未结 3 1263
遇见更好的自我
遇见更好的自我 2021-01-24 00:41

We can use std::min in below way:

// 1.
int a = 1, b = 2;
std::min(a, b);

// 2.
std::min({1,2,3,4});

But why can\'t use a std

3条回答
  •  遥遥无期
    2021-01-24 01:28

    But why can't use a std::vector or std::list, Because the param in the template is initializer_list.

    Because there is no overload that would accept a vector or a list. Those are not initialiser lists.

    Since C++20 you can use std::ranges::min into which you can pass either of those containers or indeed any range. Prior to that, there is std::min_element which works with any pair of iterators.

提交回复
热议问题