What is std::clamp?
- This ensures that the variable is within a specified range。
- References : std::clamp - cppreference.com
Ex. 1:Set variable to be in the range min~max.
- The arguments used here are shown in the table below.
1st | the variable |
---|---|
2nd | min |
3rd | max |
Source Code
#include <iostream> #include <algorithm> int main() { for (int i = 0; i < 7; i++) { std::cout << "std::clamp( " << i << ", 2, 5) = " << std::clamp(i, 2, 5) << std::endl; } }
Result
std::clamp( 0, 2, 5) = 2 std::clamp( 1, 2, 5) = 2 std::clamp( 2, 2, 5) = 2 std::clamp( 3, 2, 5) = 3 std::clamp( 4, 2, 5) = 4 std::clamp( 5, 2, 5) = 5 std::clamp( 6, 2, 5) = 5