C++ 检查较低数字的条件

C++ Condition to check for lower number with a treshold

本文关键字:条件 数字 检查 C++      更新时间:2023-10-16
肯定

有更好的方法来做到这一点,C++这个?

    if ( width_value < tipWidth )
    {
        if (tipWidth < threshold)
        {
            return threshold;
        }
        return tipWidth;
    }
    return width_value ;
    return std::max(width_value, std::max(tipWidth, threshold))

或者从 C++11 开始:

    return std::max({width_value, tipWidth, threshold})

这是假设你#include <algorithm>tipWidth width_value的类型threshold是相同的。如果不相同,则必须明确指定,例如 std::max<int>(...)