C++ std::clamp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++17

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.

C++
std::clamp(value, 0, 100);
std::clamp(value, lo, hi, comp);
CallWhat 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
C++
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

ParameterDescription
vRequired. The value to limit.
loRequired. The lowest allowed value.
hiRequired. The highest allowed value. It must not be less than lo.
compOptional. A comparison function.

Return Value

Return ValueDescription
loReturned when v is below lo.
hiReturned when v is above hi.
vReturned when v is already between the bounds.

C++ Examples

Example 1 — Three cases

A value below, above and inside the range.

C++
#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

  1. 15 is above the high limit 10, so 10 is returned.
  2. -3 is below the low limit 0, so 0 is returned.
  3. 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.

C++
#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

  1. The user asked for 130, which is too loud.
  2. clamp() returns the upper limit, 100.
  3. Assigning it back to volume keeps the setting valid.

Example 3 — Clamp a whole vector

Use clamp inside transform.

C++
#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

  1. The lambda clamps one number to the range 0 to 100.
  2. transform() applies it to every element and writes the result back.
  3. -5 and -1 become 0, 120 becomes 100, and 50 and 80 stay.

Frequently Asked Questions

In C++, std::clamp() limits a value so it stays between a low and a high bound. Include <algorithm> to use it.
Include <algorithm>. It needs C++17 or newer, so compile with -std=c++17 or later.
It returns lo if the value is below it, hi if the value is above it, and the value itself otherwise.
C++17. Compile with -std=c++17 or newer.
That is undefined behaviour. Make sure lo is not greater than hi.

Next: copy()

Copies the elements of one range into another place.

copy() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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