std::ranges:: clamp
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<algorithm>
|
||
|
调用签名
|
||
|
template
<
class
T,
class
Proj
=
std::
identity
,
std::
indirect_strict_weak_order
<
std
::
projected
<
const
T
*
, Proj
>>
Comp
=
|
(C++20 起) | |
若
std::
invoke
(
proj, v
)
的值位于
[
std::
invoke
(
proj, lo
)
,
std::
invoke
(
proj, hi
)
]
区间内,则返回
v
;否则返回最近的边界值。
当 std:: invoke ( proj, lo ) 大于 std:: invoke ( proj, hi ) 时,行为是未定义的。
本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:
目录 |
参数
| v | - | 待钳制的值 |
| lo, hi | - | 将 v 钳制到的边界范围 |
| comp | - | 应用于投影元素的比较操作 |
| proj | - | 应用于 v 、 lo 和 hi 的投影操作 |
返回值
若 v 的投影值小于 lo 的投影值,则引用 lo ;若 hi 的投影值小于 v 的投影值,则引用 hi ;否则引用 v 。
复杂度
最多进行两次比较和三次投影应用。
可能的实现
struct clamp_fn { template<class T, class Proj = std::identity, std::indirect_strict_weak_order<std::projected<const T*, Proj>> Comp = std::ranges::less> constexpr const T& operator()(const T& v, const T& lo, const T& hi, Comp comp = {}, Proj proj = {}) const { auto&& pv = std::invoke(proj, v); if (std::invoke(comp, std::forward<decltype(pv)>(pv), std::invoke(proj, lo))) return lo; if (std::invoke(comp, std::invoke(proj, hi), std::forward<decltype(pv)>(pv))) return hi; return v; } }; inline constexpr clamp_fn clamp; |
注释
std::ranges::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::ranges::clamp(n, 0, 255); // r 成为悬空引用
如果 v 与任一边界比较等价,则返回对 v 的引用,而非边界。
当投影函数返回按值传递且比较器按值接收参数时,除非从投影结果类型移动至比较器参数类型等同于复制操作,否则不应使用此函数。若通过 std::invoke 进行比较会改变投影结果,则由于 std::regular_invocable的语义要求 (已被 std::indirect_strict_weak_order 包含),该行为是未定义的。
标准要求投影结果的 值类别 必须被保留,且 proj 只能对 v 调用一次,这意味着对于比较器的两次调用,作为纯右值的投影结果必须被缓存并执行两次移动操作。
- libstdc++ 不符合此规范,始终将投影结果作为左值传递。
- libc++ 曾两次运行投影,该问题已在 Clang 18 中修正。
- MSVC STL 曾两次运行投影,该问题已在 VS 2022 17.2 中修正。
示例
#include <algorithm> #include <cstdint> #include <iomanip> #include <iostream> #include <string> using namespace std::literals; namespace ranges = std::ranges; int main() { std::cout << "[raw] [" << INT8_MIN << ',' << INT8_MAX << "] " "[0" << ',' << UINT8_MAX << "]\n"; for (int const v : {-129, -128, -1, 0, 42, 127, 128, 255, 256}) std::cout << std::setw(4) << v << std::setw(11) << ranges::clamp(v, INT8_MIN, INT8_MAX) << std::setw(8) << ranges::clamp(v, 0, UINT8_MAX) << '\n'; std::cout << std::string(23, '-') << '\n'; // 投影函数 const auto stoi = [](std::string s) { return std::stoi(s); }; // 同上,但使用字符串 for (std::string const v : {"-129", "-128", "-1", "0", "42", "127", "128", "255", "256"}) std::cout << std::setw(4) << v << std::setw(11) << ranges::clamp(v, "-128"s, "127"s, {}, stoi) << std::setw(8) << ranges::clamp(v, "0"s, "255"s, {}, stoi) << '\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 ----------------------- -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)
|
返回给定值中的较小者
(算法函数对象) |
|
(C++20)
|
返回给定值中的较大者
(算法函数对象) |
|
(C++20)
|
检查整数值是否在给定整数类型的范围内
(函数模板) |
|
(C++17)
|
将值限制在一对边界值之间
(函数模板) |