Namespaces
Variants

std::ranges:: 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
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace 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 =
ranges:: less >
constexpr const T &

clamp ( const T & v, const T & lo, const T & hi, Comp comp = { } , Proj proj = { } ) ;
(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 に適用する射影関数

戻り値

lo の射影値が v の射影値より小さい場合は lo への参照、 hi の射影値が v の射影値より小さい場合は hi への参照、それ以外の場合は v への参照。

計算量

最大で2回の比較と3回の射影の適用。

実装例

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;

注記

Capturing the result of 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 に対して一度しか呼び出せません。これは、prvalueである射影結果が比較子の2回の呼び出しに対してキャッシュされ、2回移動されなければならないことを意味します。

  • libstdc++ はこれに準拠しておらず、常に射影結果を左辺値として渡します。
  • libc++ は以前、射影を2回実行していましたが、Clang 18で修正されました。
  • MSVC STL は以前、射影を2回実行していましたが、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++17)
値を境界値のペアの間にクランプする
(関数テンプレート)