C++算法库——最小/最大操作(max, max_element

2019-07-27  本文已影响0人  霜天渔火

max:返回各给定值中的较大者

返回a与b的较大者

template< class T > 
constexpr const T& max( const T& a, const T& b );
template< class T, class Compare >
constexpr const T& max( const T& a, const T& b, Compare cmp );

返回初始化列表中值的最大者

template< class T >
constexpr T max( std::initializer_list<T> ilist );
template< class T, class Compare >
constexpr T max( std::initializer_list<T> ilist, Compare comp );
const string str = max( { "foo", "bar", "hello" },
    [](const string& s1, const string& s2) {
        return s1.size() < s2.size();
    });

max_element:返回范围内的最大元素

template< class ForwardIt > 
constexpr ForwardIt max_element(ForwardIt first, ForwardIt last );
template< class ForwardIt, class Compare >
constexpr ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp );
vector<int> v{ 1, 2, 3 };
int result = *max_element( v.begin(), b.end() );

clamp:裁剪到给定范围

template<class T>
constexpr const T& clamp( const T& v, const T& lo, const T& hi );
template<class T, class Compare>
constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp );
上一篇下一篇

猜你喜欢

热点阅读