Home C++ Algorithms Functions clamp() C++ std::clamp() Function Definition In C++17, std::clamp() keeps a value inside a range. If the value is below the low limit, you get the low limit. If it is above the high limit, you get the high limit. Otherwise the value is returned unchanged.
It is perfect for sliders, volume levels, percentages and colour values.
Syntax Add #include <algorithm>, then call std::clamp(). Start with these two lines.
std::clamp(value, 0, 100);
std::clamp(value, lo, hi, comp); Call What it does std::clamp(value, 0, 100);Keep value between 0 and 100. std::clamp(value, lo, hi, comp);Use your own comparison.
Compile with -std=c++17 or newer.
Library declarations template<class T>
const T& clamp(const T& v, const T& lo, const T& hi);
template<class T, class Compare>
const T& clamp(const T& v, const T& lo, const T& hi, Compare comp); Parameters Parameter Description v Required. The value to limit. lo Required. The lowest allowed value. hi Required. The highest allowed value. It must not be less than lo. comp Optional. A comparison function.
Return Value Return Value Description loReturned when v is below lo. hiReturned when v is above hi. vReturned when v is already between the bounds.
C++ Examples 1. Three cases 2. Volume control 3. Clamp a whole vector Example 1 — Three cases A value below, above and inside the range.
#include <algorithm>
#include <iostream>
int main() {
std::cout << std::clamp(15, 0, 10) << '\n';
std::cout << std::clamp(-3, 0, 10) << '\n';
std::cout << std::clamp(5, 0, 10) << '\n';
return 0;
} How It Works 15 is above the high limit 10, so 10 is returned. -3 is below the low limit 0, so 0 is returned. 5 is already between the limits, so it is returned as it is. Example 2 — Volume control Keep a volume setting between 0 and 100.
#include <algorithm>
#include <iostream>
int main() {
int volume = 130;
volume = std::clamp(volume, 0, 100);
std::cout << "Volume: " << volume << '\n';
return 0;
} How It Works The user asked for 130, which is too loud. clamp() returns the upper limit, 100.Assigning it back to volume keeps the setting valid. Example 3 — Clamp a whole vector Use clamp inside transform.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {-5, 50, 120, 80, -1};
std::transform(v.begin(), v.end(), v.begin(),
[](int n) { return std::clamp(n, 0, 100); });
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
} How It Works The lambda clamps one number to the range 0 to 100. transform() applies it to every element and writes the result back.-5 and -1 become 0, 120 becomes 100, and 50 and 80 stay. Frequently Asked Questions What does std::clamp() do? In C++, std::clamp() limits a value so it stays between a low and a high bound. Include <algorithm> to use it.
Which header and C++ version do I need for std::clamp()? Include <algorithm>. It needs C++17 or newer, so compile with -std=c++17 or later.
What does std::clamp() return? It returns lo if the value is below it, hi if the value is above it, and the value itself otherwise.
Which C++ version adds std::clamp()? C++17. Compile with -std=c++17 or newer.
What if lo is greater than hi? That is undefined behaviour. Make sure lo is not greater than hi.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
19 people found this page helpful
Helpful Share Copy link Suggestion