C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- std::execution::sequenced_policy, std::execution::parallel_policy, std::execution::parallel_unsequenced_policy, std::execution::unsequenced_policy
- æå¶çº¦ç®æ³ (C++20 èµ·)
- std::is_execution_policy
- std::execution::seq, std::execution::par, std::execution::par_unseq, std::execution::unseq
- std::all_of, std::any_of, std::none_of
- std::for_each_n
- std::sort
- std::reduce
- std::inclusive_scan
- std::exclusive_scan
- std::random_shuffle, std::shuffle
- std::clamp
- std::equal
- std::is_permutation
- std::mismatch
- std::swap
- std::search
- std::transform
- std::for_each
- std::make_heap
- std::count, std::count_if
- std::adjacent_find
- std::find, std::find_if, std::find_if_not
- std::find_end
- std::find_first_of
- std::search_n
- std::lexicographical_compare
- std::lexicographical_compare_three_way
- std::copy, std::copy_if
- std::copy_n
- std::copy_backward
- std::move
- std::move_backward
- std::shift_left, std::shift_right
- std::fill
- std::fill_n
- std::generate
- std::generate_n
- std::iter_swap
- std::swap_ranges
- std::sample
- std::remove, std::remove_if
- std::replace, std::replace_if
- std::reverse
- std::rotate
- std::unique
- std::remove_copy, std::remove_copy_if
- std::replace_copy, std::replace_copy_if
- std::reverse_copy
- std::rotate_copy
- std::unique_copy
- std::is_partitioned
- std::partition_point
- std::partition
- std::partition_copy
- std::stable_partition
- std::is_sorted
- std::is_sorted_until
- std::stable_sort
- std::partial_sort
- std::partial_sort_copy
- std::nth_element
- std::lower_bound
- std::upper_bound
- std::binary_search
- std::equal_range
- std::merge
- std::inplace_merge
- std::set_difference
- std::set_intersection
- std::set_symmetric_difference
- std::set_union
- std::includes
- std::is_heap
- std::is_heap_until
- std::sort_heap
- std::push_heap
- std::pop_heap
- std::max
- std::max_element
- std::min
- std::min_element
- std::minmax
- std::minmax_element
- std::next_permutation
- std::prev_permutation
- std::iota
- std::inner_product
- std::adjacent_difference
- std::accumulate
- std::transform_reduce
- std::partial_sum
- std::transform_inclusive_scan
- std::transform_exclusive_scan
- std::qsort
- std::bsearch
- 注é
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >ç®æ³åº > std::clamp
std::clamp
| å®ä¹äºå¤´æä»¶ <algorithm>
|
||
| template<class T> constexpr const T& clamp( const T& v, const T& lo, const T& hi ); |
(1) | (C++17 èµ·) |
| template<class T, class Compare> constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); |
(2) | (C++17 èµ·) |
v å°äº lo ï¼åè¿å lo ï¼è¥ v å¤§äº hi ï¼åè¿å hi ï¼å¦åè¿å v ãä½¿ç¨ operator< æ¯è¾å¼ãcomp æ¯è¾å¼ãè¥ lo çå¼å¤§äº hi åè¡ä¸ºæªå®ä¹ã
åæ°
| v | - | è¦å¤¹ä½çå¼ |
| lo,hi | - | 夹 v çè¾¹ç
|
| comp | - | æ¯è¾å½æ°å¯¹è±¡ï¼å³æ»¡è¶³æ¯è¾ (Compare) è¦æ±ç对象ï¼ï¼è¥ lo å°äº v ä¸ v å°äº hiï¼åè¿å âtrue ãæ¯è¾å½æ°çç¾ååºçä»·äºå¦ä¸ï¼  bool cmp(const Type1 &a, const Type2 &b); è½ç¶ç¾åä¸å¿
æ const & ï¼å½æ°ä¹ä¸è½ä¿®æ¹ä¼ éç»å®ç对象ï¼èä¸å¿
é¡»æ¥åï¼å¯ä¸º const çï¼ç±»å |
| ç±»åè¦æ± | ||
- 为使ç¨éè½½ (1) ï¼ T å¿
须满足å¯å°äºæ¯è¾ (LessThanComparable) çè¦æ±ãç¶èè¥é¿å
äº NaN ï¼å T è½ä¸ºæµ®ç¹ç±»åã
| ||
è¿åå¼
è¥ v å°äº lo å°å为 lo çå¼ç¨ï¼è¥ hi å°äº v åä¸ºå° hi çå¼ç¨ï¼å¦åä¸ºå° v çå¼ç¨ã
å¤æåº¦
å¯è½çå®ç°
| çæ¬ä¸ |
|---|
template<class T> constexpr const T& clamp( const T& v, const T& lo, const T& hi ) { assert( !(hi < lo) ); return v < lo ? lo : hi < v ? hi : v; } |
| çæ¬äº |
template<class T, class Compare> constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ) { assert( !comp(hi, lo) ); return comp(v, lo) ? lo : comp(hi, v) ? hi : v; } |
注æ
std::clamp çç»æä¼äº§çä¸ä¸ªæ¬åå¼ç¨ï¼
int n = -1; const int& r = std::clamp(n, 0, 255); // r 为æ¬å
è¥ v ä¸è¾¹çä¹ä¸æ¯è¾ç¸çï¼åè¿åå° v çå¼ç¨ï¼èéå°è¾¹çè
ã
示ä¾
#include <cstdint> #include <algorithm> #include <iostream> #include <iomanip> #include <random> Â int main() { std::mt19937 g(std::random_device{}()); std::uniform_int_distribution<> d(-300, 300); std::cout << " raw clamped to int8_t clamped to uint8_t\n"; for(int n = 0; n < 5; ++n) { int v = d(g); std::cout << std::setw(4) << v << std::setw(20) << std::clamp(v, INT8_MIN, INT8_MAX) << std::setw(21) << std::clamp(v, 0, UINT8_MAX) << '\n'; } }
å¯è½çè¾åºï¼
.raw clamped to int8_t clamped to uint8_t 168 127 168 128 127 128 -137 -128 0 40 40 40 -66 -66 0
åé
| è¿ååç»å®å¼ä¸çè¾å°è
(彿°æ¨¡æ¿) | |
| è¿ååç»å®å¼ä¸çè¾å¤§è
(彿°æ¨¡æ¿) |