當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++17 std::clamp用法及代碼示例


將變量限製在給定範圍[高 - 低]。

If num > high, num is assigned high.
If num < low, num is assigned low.
If num is already clamped, no modifications.

注意:從 C++17 開始,此函數在頭文件中定義。

例子:

Input : num = 100, Range : 10 - 90
Output : num = 90

Input : num = 5, Range : 10 - 90
Output : num = 10

Input : num = 50, Range : 10 - 90
Output : num = 50

用法:

templateconstexpr const T& clamp( const T& num, const T& low, const T& high );
template
constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp );
參數
num      -    the value to clamp
low, high  -    the boundaries to clamp num to
comp      -    comparison function object which returns ?true if low is less than num
                and num is less than high. 

Return value
Reference to low if num is less than low,
Reference to high if high is less than num,
otherwise reference to num.

Applications :
To keep a list of inputs specified to a given range. 
Preventing size overflow.

// CPP program to illustrate 
// std::clamp 
#include <bits/stdc++.h> 
  
// Driver code 
int main() 
{ 
    // range [10 - 100] 
    int high = 100, low = 10; 
  
    // num1 higher than range 
    int num1 = 120; 
  
    // num2 lower than range 
    int num2 = 5; 
  
    // num3 in range 
    int num3 = 50; 
  
    // clamping all variables 
    num1 = std::clamp(num1, low, high); 
    num2 = std::clamp(num2, low, high); 
    num3 = std::clamp(num3, low, high); 
  
    // printing result 
    std::cout << num1 << " " << num2 << " " << num3; 
} 

輸出:

100 10 50

// CPP program to illustrate clamping 
// array elements in given range 
#include <iostream> 
#include <algorithm> 
  
// Range [30 - 60] 
int low = 30, high = 60; 
  
// Function to clamp the elements in given range 
void clamp_arr_in_range(int arr[], int n) 
{ 
    // Clamping the array elements 
    for (int i = 0; i < n; i++) { 
        arr[i] = std::clamp(arr[i], low, high); 
    } 
} 
  
// Driver code 
int main() 
{ 
    // Array having elements to be clamped 
    int arr[] = { 10, 20, 30, 40, 50, 60, 70, 80 }; 
  
    // Size of array 
    int n = sizeof(arr) / sizeof(arr[0]); 
  
    // Function call to clamp the elements 
    clamp_arr_in_range(arr, n); 
  
    // Printing the array 
    for (int i = 0; i < n; i++) 
        std::cout << arr[i] << " "; 
} 

輸出:

30 30 30 40 50 60 60 60

With Binary Predicate


// C++ program to implement std::clamp 
// with Binary Predicate 
  
#include <bits/stdc++.h> 
  
// Binary predicate 
bool comp(int a, int b) 
{ 
    return (a < b); 
} 
  
// Driver code 
int main() 
{ 
    // range [10 - 100] 
    int high = 100, low = 10; 
  
    // num1 higher than range 
    int num1 = 120; 
  
    // num2 lower than range 
    int num2 = 5; 
  
    // num3 in range 
    int num3 = 50; 
  
    // clamping all variables 
    num1 = std::clamp(num1, low, high, comp); 
    num2 = std::clamp(num2, low, high, comp); 
    num3 = std::clamp(num3, low, high, comp); 
  
    // printing result 
    std::cout << num1 << " " << num2 << " " << num3; 
} 

輸出:

100 10 50


相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 std::clamp in C++ 17。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。