Namespaces
Variants

std:: clamp

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
clamp
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
template < class T >
constexpr const T & clamp ( const T & v, const T & lo, const T & hi ) ;
(1) (C++17 起)
template < class T, class Compare >

constexpr const T & clamp ( const T & v, const T & lo, const T & hi,

Compare comp ) ;
(2) (C++17 起)

v 的值在 [ lo , hi ] 区间内,则返回 v ;否则返回最近的边界值。

1) 使用 operator < (C++20 前) std:: less { } (C++20 起) 来比较值。
如果 T 不满足 LessThanComparable 要求,则行为未定义。 [1]
2) 使用比较函数 comp 来比较值。

如果 lo 大于 hi ,其行为是未定义的。

  1. 若避免使用 NaN ,则 T 可为浮点类型。

目录

参数

v - 需要夹取的值
lo, hi - v 的夹取边界
comp - 比较函数对象(即满足 Compare 要求的对象),当第一个参数 小于 第二个参数时返回 true

比较函数的签名应等价于如下形式:

bool cmp ( const Type1 & a, const Type2 & b ) ;

虽然签名不需要包含 const & ,但函数不得修改传递给它的对象,且必须能够接受所有(可能为 const 的) Type1 Type2 类型的值,无论其 值类别 为何(因此不允许 Type1 & ,也不允许 Type1 ,除非对于 Type1 类型移动操作等价于拷贝操作 (C++11 起) )。
类型 Type1 Type2 必须使得 T 类型的对象能隐式转换到这两个类型。

返回值

v 小于 lo 则引用 lo ,若 hi 小于 v 则引用 hi ,否则引用 v

复杂度

1) 最多进行两次比较,使用 operator < (C++20 前) std:: less { } (C++20 起)
2) 最多两次比较函数 comp 的应用。

可能的实现

clamp (1)
template<class T>
constexpr const T& clamp(const T& v, const T& lo, const T& hi)
{
    return clamp(v, lo, hi, less{});
}
clamp (2)
template<class T, class Compare>
constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp)
{
    return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
}

注释

Capturing the result of std::clamp by reference produces a dangling reference if one of the parameters is a temporary and that parameter is returned:
int n = -1;
const int& r = std::clamp(n, 0, 255); // r 是悬空引用

如果 v 与任一边界值等价比较,则返回对 v 的引用,而非边界值引用。

功能测试 标准 功能
__cpp_lib_clamp 201603L (C++17) std::clamp

示例

#include <algorithm>
#include <cstdint>
#include <iomanip>
#include <iostream>
int main()
{
    std::cout << "[raw] "
                 "[" << INT8_MIN << ',' << INT8_MAX << "] "
                 "[0," << UINT8_MAX << "]\n";
    for (const int v : {-129, -128, -1, 0, 42, 127, 128, 255, 256})
        std::cout << std::setw(4) << v
                  << std::setw(11) << std::clamp(v, INT8_MIN, INT8_MAX)
                  << std::setw(8) << std::clamp(v, 0, UINT8_MAX) << '\n';
}

输出:

[raw] [-128,127] [0,255]
-129       -128       0
-128       -128       0
  -1         -1       0
   0          0       0
  42         42      42
 127        127     127
 128        127     128
 255        127     255
 256        127     255

参见

返回给定值中的较小者
(函数模板)
返回给定值中的较大者
(函数模板)
(C++20)
检查整数值是否在给定整数类型的范围内
(函数模板)
将值限制在一对边界值之间
(算法函数对象)