initializer_list
1 构造函数场景
cpp
class P {
public:
P(int a, int b) {
std::cout << "int, int" << std::endl;
}
P(std::initializer_list<int> initList) {
std::cout << "initializer_list" << std::endl;
}
};
调用:
cpp
P p(1, 2);
P q{1, 2};
P r{1, 2, 4};
P s = {1, 2};
shell
int, int
initializer_list
initializer_list
initializer_list
2 算法场景
cpp
min({1, 2, 3});
max({1, 2, 3});
源码
cpp
// N2722 + DR 915.
template<typename _Tp>
_GLIBCXX14_CONSTEXPR
inline _Tp
min(initializer_list<_Tp> __l)
{ return *std::min_element(__l.begin(), __l.end()); }
template<typename _Tp, typename _Compare>
_GLIBCXX14_CONSTEXPR
inline _Tp
min(initializer_list<_Tp> __l, _Compare __comp)
{ return *std::min_element(__l.begin(), __l.end(), __comp); }
template<typename _Tp>
_GLIBCXX14_CONSTEXPR
inline _Tp
max(initializer_list<_Tp> __l)
{ return *std::max_element(__l.begin(), __l.end()); }
template<typename _Tp, typename _Compare>
_GLIBCXX14_CONSTEXPR
inline _Tp
max(initializer_list<_Tp> __l, _Compare __comp)
{ return *std::max_element(__l.begin(), __l.end(), __comp); }