Most efficient/elegant way to clip a number?

前端 未结 11 733
予麋鹿
予麋鹿 2020-11-30 03:09

Given a real (n), a maximum value this real can be (upper), and a minimum value this real can be (lower), how can we most efficiently clip n, such that it remains between lo

11条回答
  •  粉色の甜心
    2020-11-30 03:45

    C++17 is expected to add a clamp function. Courtesy of cppreference.com:

    template
    constexpr const T& clamp( const T& v, const T& lo, const T& hi );
    
    template
    constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp );
    

提交回复
热议问题