std::ranges::lexicographical_compare
| (C++23) <algorithm>
|
||
| 헤더 |
||
template< std::input_iterator I1, std::sentinel_for<I1> S1,
std::input_iterator I2, std::sentinel_for<I2> S2,
class Proj1 = std::identity, class Proj2 = std::identity,
std::indirect_strict_weak_order<
std::projected<I1, Proj1>,
std::projected<I2, Proj2>> Comp = ranges::less >
constexpr bool
lexicographical_compare( I1 first1, S1 last1, I2 first2, S2 last2,
Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
|
에 정의됨 | 호출 시그니처 |
template< ranges::input_range R1, ranges::input_range R2,
class Proj1 = std::identity, class Proj2 = std::identity,
std::indirect_strict_weak_order<
std::projected<ranges::iterator_t<R1>, Proj1>,
std::projected<ranges::iterator_t<R2>, Proj2>> Comp = ranges::less >
constexpr bool
lexicographical_compare( R1&& r1, R2&& r2, Comp comp = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
|
(1) | (C++20부터) |
(2) [first1, last1)(C++20부터)첫 번째 범위 가 두 번째 범위 [first2, last2)보다 사전식으로
comp
r (1)ranges::begin(r)과 동일하지만, first를 소스 범위로 사용합니다. 이는 마치 ranges::end(r)를 last으로, 를
- 로 사용하는 것과 같습니다.
- 사전식 비교는 다음과 같은 속성을 가진 연산입니다: 두 범위는 요소별로 비교됩니다.첫 번째로 일치하지 않는 요소가 어떤 범위가 사전식으로 작은지 또는
- 큰지를 결정합니다.한 범위가 다른 범위의 접두사라면, 더 짧은 범위가 사전식으로
- 작습니다.두 범위가 동등한 요소들을 가지고 길이가 같다면, 두 범위는 사전식으로 같습니다
- .빈 범위는 비어 있지 않은 범위보다 사전식으로 작습니다.
- 두 빈 범위는 사전식으로 같습니다.
이 페이지에 설명된 함수형 개체들은 알고리즘 함수 객체 (비공식적으로 niebloids라고 함)입니다. 즉:
- 이들 중 하나를 호출할 때 명시적 템플릿 인자 목록을 지정할 수 없습니다.
- 이들 중 어느 것도 인자 종속 조회에 보이지 않습니다.
- 이들 중 하나가 일반적인 비한정 조회에 의해 함수 호출 연산자의 왼쪽 이름으로 발견되면, 인자 종속 조회가 금지됩니다.
매개변수
| first1, last1 | - | 검사할 첫 번째 범위 를 정의하는 반복자-감시자 쌍 |
| r1 | - | 검사할 첫 번째 요소 범위 |
| first2, last2 | - | 검사할 두 번째 범위 를 정의하는 반복자-감시자 쌍 |
| r2 | - | 검사할 두 번째 요소 범위 |
| comp | - | 투영된 요소에 적용할 비교 함수 |
| proj1 | - | 첫 번째 요소 범위에 적용할 투영 |
| proj2 | - | 두 번째 요소 범위에 적용할 투영 |
반환값
true 첫 번째 범위가 두 번째 범위보다 사전순으로 작은 경우.
복잡도
최대 2·min(N1, N2) 번의 비교 연산과 해당 프로젝션 연산이 수행되며, 여기서 N1 = ranges:: distance ( first1, last1 ) 이고 N2 = ranges:: distance ( first2, last2 ) 입니다.
가능한 구현
struct lexicographical_compare_fn { template<std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, class Proj1 = std::identity, class Proj2 = std::identity, std::indirect_strict_weak_order< std::projected<I1, Proj1>, std::projected<I2, Proj2>> Comp = ranges::less> constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { for (; (first1 != last1) && (first2 != last2); ++first1, (void) ++first2) { if (std::invoke(comp, std::invoke(proj1, *first1), std::invoke(proj2, *first2))) return true; if (std::invoke(comp, std::invoke(proj2, *first2), std::invoke(proj1, *first1))) return false; } return (first1 == last1) && (first2 != last2); } template<ranges::input_range R1, ranges::input_range R2, class Proj1 = std::identity, class Proj2 = std::identity, std::indirect_strict_weak_order< std::projected<ranges::iterator_t<R1>, Proj1>, std::projected<ranges::iterator_t<R2>, Proj2>> Comp = ranges::less> constexpr bool operator()(R1&& r1, R2&& r2, Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), std::ref(comp), std::ref(proj1), std::ref(proj2)); } }; inline constexpr lexicographical_compare_fn lexicographical_compare; |
예제
#include <algorithm> #include <iostream> #include <iterator> #include <random> #include <vector> int main() { std::vector<char> v1 {'a', 'b', 'c', 'd'}; std::vector<char> v2 {'a', 'b', 'c', 'd'}; namespace ranges = std::ranges; auto os = std::ostream_iterator<char>(std::cout, " "); std::mt19937 g {std::random_device {}()}; while (not ranges::lexicographical_compare(v1, v2)) { ranges::copy(v1, os); std::cout << ">= "; ranges::copy(v2, os); std::cout << '\n'; ranges::shuffle(v1, g); ranges::shuffle(v2, g); } ranges::copy(v1, os); std::cout << "< "; ranges::copy(v2, os); std::cout << '\n'; }
가능한 출력:
a b c d >= a b c d d a b c >= c b d a b d a c >= a d c b a c d b < c d a b
참고 항목
(C++20) |
두 요소 집합이 같은지 판별합니다 (알고리즘 함수 객체) |
| 두 범위를 사전순으로 비교합니다 (함수 템플릿) |