std::ranges:: minmax, std::ranges:: minmax_result
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
헤더에 정의됨
<algorithm>
|
||
|
호출 시그니처
|
||
|
template
<
class
T,
class
Proj
=
std::
identity
,
std::
indirect_strict_weak_order
<
|
(1) | (C++20 이후) |
|
template
<
std::
copyable
T,
class
Proj
=
std::
identity
,
std::
indirect_strict_weak_order
<
|
(2) | (C++20 이후) |
|
template
<
ranges::
input_range
R,
class
Proj
=
std::
identity
,
std::
indirect_strict_weak_order
<
|
(3) | (C++20 이후) |
|
헬퍼 타입
|
||
|
template
<
class
T
>
using minmax_result = ranges:: min_max_result < T > ; |
(4) | (C++20 이후) |
주어진 투영된 값들 중 가장 작은 값과 가장 큰 값을 반환합니다.
이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:
- 명시적 템플릿 인수 목록은 이들 중 어느 것을 호출할 때도 지정할 수 없습니다.
- 이들 중 어느 것도 인수 의존 이름 검색 에 보이지 않습니다.
- 이들 중 어느 것이 함수 호출 연산자 왼쪽의 이름으로 일반 비한정 이름 검색 에 의해 발견될 때, 인수 의존 이름 검색 이 억제됩니다.
목차 |
매개변수
| a, b | - | 비교할 값 |
| r | - | 비교할 값들의 비어 있지 않은 범위 |
| comp | - | 투영된 요소에 적용할 비교 연산 |
| proj | - | 요소에 적용할 투영 연산 |
반환값
s
와
l
는 각각
r
내에서 투영된 값에 따라 가장 작은 값과 가장 큰 값입니다. 여러 값이 가장 작거나 가장 큰 값과 동등한 경우, 가장 왼쪽의 가장 작은 값과 가장 오른쪽의 가장 큰 값을 반환합니다. 범위가 비어 있는 경우(
ranges::
distance
(
r
)
로 결정됨), 동작은 정의되지 않습니다.
복잡도
가능한 구현
struct minmax_fn { template<class T, class Proj = std::identity, std::indirect_strict_weak_order< std::projected<const T*, Proj>> Comp = ranges::less> constexpr ranges::minmax_result<const T&> operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const { if (std::invoke(comp, std::invoke(proj, b), std::invoke(proj, a))) return {b, a}; return {a, b}; } template<std::copyable T, class Proj = std::identity, std::indirect_strict_weak_order< std::projected<const T*, Proj>> Comp = ranges::less> constexpr ranges::minmax_result<T> operator()(std::initializer_list<T> r, Comp comp = {}, Proj proj = {}) const { auto result = ranges::minmax_element(r, std::ref(comp), std::ref(proj)); return {*result.min, *result.max}; } template<ranges::input_range R, class Proj = std::identity, std::indirect_strict_weak_order< std::projected<ranges::iterator_t<R>, Proj>> Comp = ranges::less> requires std::indirectly_copyable_storable<ranges::iterator_t<R>, ranges::range_value_t<R>*> constexpr ranges::minmax_result<ranges::range_value_t<R>> operator()(R&& r, Comp comp = {}, Proj proj = {}) const { auto result = ranges::minmax_element(r, std::ref(comp), std::ref(proj)); return {std::move(*result.min), std::move(*result.max)}; } }; inline constexpr minmax_fn minmax; |
참고 사항
오버로드
(1)
의 경우, 매개변수 중 하나가 임시 객체인 경우,
minmax
호출을 포함하는 전체 표현식의 끝에서 반환된 참조는 댕글링 참조가 됩니다:
int n = 1; auto p = std::ranges::minmax(n, n + 1); int m = p.min; // 정상 int x = p.max; // 정의되지 않은 동작 // 구조화된 바인딩도 동일한 문제가 있음 auto [mm, xx] = std::ranges::minmax(n, n + 1); xx; // 정의되지 않은 동작
예제
#include <algorithm> #include <array> #include <iostream> #include <random> int main() { namespace ranges = std::ranges; constexpr std::array v{3, 1, 4, 1, 5, 9, 2, 6, 5}; std::random_device rd; std::mt19937_64 generator(rd()); std::uniform_int_distribution<> distribution(0, ranges::distance(v)); // [0..9] // auto bounds = ranges::minmax(distribution(generator), distribution(generator)); // UB: dangling references: bounds.min and bounds.max have the type `const int&`. const int x1 = distribution(generator); const int x2 = distribution(generator); auto bounds = ranges::minmax(x1, x2); // OK: got references to lvalues x1 and x2 std::cout << "v[" << bounds.min << ":" << bounds.max << "]: "; for (int i = bounds.min; i < bounds.max; ++i) std::cout << v[i] << ' '; std::cout << '\n'; auto [min, max] = ranges::minmax(v); std::cout << "smallest: " << min << ", " << "largest: " << max << '\n'; }
가능한 출력:
v[3:9]: 1 5 9 2 6 5 smallest: 1, largest: 9
참고 항목
|
(C++20)
|
주어진 값들 중 더 작은 값을 반환합니다
(알고리즘 함수 객체) |
|
(C++20)
|
주어진 값들 중 더 큰 값을 반환합니다
(알고리즘 함수 객체) |
|
(C++20)
|
범위에서 가장 작은 요소와 가장 큰 요소를 반환합니다
(알고리즘 함수 객체) |
|
(C++20)
|
값을 한 쌍의 경계값 사이로 고정합니다
(알고리즘 함수 객체) |
|
(C++11)
|
두 요소 중 더 작은 값과 더 큰 값을 반환합니다
(함수 템플릿) |