mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-17 01:54:36 +03:00
Add std::clamp
This commit is contained in:
12
CPP17.md
12
CPP17.md
@@ -32,6 +32,7 @@ C++17 includes the following new library features:
|
|||||||
- [splicing for maps and sets](#splicing-for-maps-and-sets)
|
- [splicing for maps and sets](#splicing-for-maps-and-sets)
|
||||||
- [parallel algorithms](#parallel-algorithms)
|
- [parallel algorithms](#parallel-algorithms)
|
||||||
- [std::sample](#stdsample)
|
- [std::sample](#stdsample)
|
||||||
|
- [std::clamp](#stdclamp)
|
||||||
|
|
||||||
## C++17 Language Features
|
## C++17 Language Features
|
||||||
|
|
||||||
@@ -536,6 +537,17 @@ std::sample(ALLOWED_CHARS.begin(), ALLOWED_CHARS.end(), std::back_inserter(guid)
|
|||||||
std::cout << guid; // e.g. G1fW2
|
std::cout << guid; // e.g. G1fW2
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### std::clamp
|
||||||
|
Clamp given value between a lower and upper bound.
|
||||||
|
```c++
|
||||||
|
std::clamp(42, -1, 1); // == 1
|
||||||
|
std::clamp(-42, -1, 1); // == -1
|
||||||
|
std::clamp(0, -1, 1); // == 0
|
||||||
|
|
||||||
|
// `std::clamp` also accepts a custom comparator:
|
||||||
|
std::clamp(0, -1, 1, std::less<>{}); // == 0
|
||||||
|
```
|
||||||
|
|
||||||
## Acknowledgements
|
## Acknowledgements
|
||||||
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
|
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
|
||||||
* [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.
|
* [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.
|
||||||
|
|||||||
12
README.md
12
README.md
@@ -62,6 +62,7 @@ C++17 includes the following new library features:
|
|||||||
- [splicing for maps and sets](#splicing-for-maps-and-sets)
|
- [splicing for maps and sets](#splicing-for-maps-and-sets)
|
||||||
- [parallel algorithms](#parallel-algorithms)
|
- [parallel algorithms](#parallel-algorithms)
|
||||||
- [std::sample](#stdsample)
|
- [std::sample](#stdsample)
|
||||||
|
- [std::clamp](#stdclamp)
|
||||||
|
|
||||||
C++14 includes the following new language features:
|
C++14 includes the following new language features:
|
||||||
- [binary literals](#binary-literals)
|
- [binary literals](#binary-literals)
|
||||||
@@ -1200,6 +1201,17 @@ std::sample(ALLOWED_CHARS.begin(), ALLOWED_CHARS.end(), std::back_inserter(guid)
|
|||||||
std::cout << guid; // e.g. G1fW2
|
std::cout << guid; // e.g. G1fW2
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### std::clamp
|
||||||
|
Clamp given value between a lower and upper bound.
|
||||||
|
```c++
|
||||||
|
std::clamp(42, -1, 1); // == 1
|
||||||
|
std::clamp(-42, -1, 1); // == -1
|
||||||
|
std::clamp(0, -1, 1); // == 0
|
||||||
|
|
||||||
|
// `std::clamp` also accepts a custom comparator:
|
||||||
|
std::clamp(0, -1, 1, std::less<>{}); // == 0
|
||||||
|
```
|
||||||
|
|
||||||
## C++14 Language Features
|
## C++14 Language Features
|
||||||
|
|
||||||
### Binary literals
|
### Binary literals
|
||||||
|
|||||||
Reference in New Issue
Block a user