Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::clamp

      From cppreference.com
      <cpp‎ |algorithm
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(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
       
      Defined in header<algorithm>
      template<class T>
      constexprconst T& clamp(const T& v,const T& lo,const T& hi);
      (1)(since C++17)
      template<class T,class Compare>

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

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

      If the value ofv is within[lohi], returnsv; otherwise returns the nearest boundary.

      1) Usesoperator<(until C++20)std::less{}(since C++20) to compare the values.
      IfT is notLessThanComparable, the behavior is undefined.[1]
      2) Uses the comparison functioncomp to compare the values.

      Iflo is greater thanhi, the behavior is undefined.

      1. IfNaN is avoided,T can be a floating-point type.

      Contents

      [edit]Parameters

      v - the value to clamp
      lo, hi - the boundaries to clampv to
      comp - comparison function object (i.e. an object that satisfies the requirements ofCompare) which returnstrue if the first argument isless than the second.

      The signature of the comparison function should be equivalent to the following:

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

      While the signature does not need to haveconst&, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const)Type1 andType2 regardless ofvalue category (thus,Type1& is not allowed, nor isType1 unless forType1 a move is equivalent to a copy(since C++11)).
      The typesType1 andType2 must be such that an object of typeT can be implicitly converted to both of them.

      [edit]Return value

      Reference tolo ifv is less thanlo, reference tohi ifhi is less thanv, otherwise reference tov.

      [edit]Complexity

      1) At most two comparisons usingoperator<(until C++20)std::less{}(since C++20).
      2) At most two applications of the comparison functioncomp.

      [edit]Possible implementation

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

      [edit]Notes

      [edit]
      Capturing the result ofstd::clamp by reference produces a dangling reference if one of the parameters is a temporary and that parameter is returned:
      int n=-1;constint& r= std::clamp(n,0,255);// r is dangling

      Ifv compares equivalent to either bound, returns a reference tov, not the bound.

      Feature-test macroValueStdFeature
      __cpp_lib_clamp201603L(C++17)std::clamp

      [edit]Example

      Run this code
      #include <algorithm>#include <cstdint>#include <iomanip>#include <iostream> int main(){std::cout<<"[raw] ""["<<INT8_MIN<<','<<INT8_MAX<<"] ""[0,"<<UINT8_MAX<<"]\n"; for(constint 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';}

      Output:

      [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

      [edit]See also

      returns the smaller of the given values
      (function template)[edit]
      returns the greater of the given values
      (function template)[edit]
      (C++20)
      checks if an integer value is in the range of a given integer type
      (function template)[edit]
      clamps a value between a pair of boundary values
      (algorithm function object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/clamp&oldid=171801"

      [8]ページ先頭

      ©2009-2025 Movatter.jp