#include <algorithm>
const T& clamp (const T& value, const T& low, const T& high) // C++17 const T& clamp (const T& value, const T& low, const T& high, Binary comp)
Begrenzt Wert auf das Intervall [low,high].
value | Wert |
low | Untergrenze |
high | Obergrenze |
comp | Vergleichsoperator |
Rückgabewert: comp(value, low) ? low : comp(high, value) ? high : value,
bzw.
low, wenn value < low,
high, wenn high < value,
sonst value.
#include <algorithm> #include <iostream> int main() { for (auto x : { -2, 0, 1, 255, 256 }) std::cout << std::clamp(x, 0, 255) << '\n'; }