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 < (until C++20) std:: less { } (since C++20) を使用します。
T LessThanComparable でない場合、動作は未定義です。 [1]
2) 比較関数 comp を使用して値を比較します。

lo hi より大きい場合、動作は未定義です。

  1. NaN が回避される場合、 T は浮動小数点型でも可能です。

目次

翻訳のポイント: - 「Contents」を「目次」に翻訳 - HTMLタグ、属性、リンク先は一切変更せず保持 - C++関連の用語(Parameters、Return value、Complexity、Possible implementation、Notes、Example、See also)は原文のまま保持 - 数字や構造は完全に維持 - プロフェッショナルな技術文書としての正確性を確保

パラメータ

v - クランプする値
lo, hi - v をクランプする境界値
comp - 比較関数オブジェクト(つまり Compare 要件を満たすオブジェクト)。最初の引数が2番目の引数より 小さい 場合に true を返す。

比較関数のシグネチャは以下と同等であるべき:

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

シグネチャに const & が含まれている必要はないが、関数は渡されたオブジェクトを変更してはならず、 値カテゴリ に関係なく型(おそらくconstの) Type1 Type2 のすべての値を受け入れられなければならない(したがって Type1 & は許可されない 。また Type1 も、 Type1 においてムーブがコピーと等価である場合を除き許可されない (C++11以降) )。
Type1 Type2 は、 T 型のオブジェクトが両方に暗黙変換可能でなければならない。

戻り値

lo v より大きい場合は lo を参照し、 hi v より小さい場合は hi を参照し、それ以外の場合は v を参照します。

計算量

1) 最大2回の比較を使用して operator < (C++20以前) std:: less { } (C++20以降)
2) 比較関数 comp の適用は最大2回まで。

実装例

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)
整数値が指定された整数型の範囲内にあるかどうかをチェックする
(関数テンプレート)
値を境界値のペアの間にクランプする
(アルゴリズム関数オブジェクト)