std:: lexicographical_compare_three_way
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
InputIt1,
class
InputIt2,
class
Cmp
>
constexpr
auto
lexicographical_compare_three_way
|
(1) | (C++20부터) |
|
template
<
class
InputIt1,
class
InputIt2
>
constexpr
auto
lexicographical_compare_three_way
|
(2) | (C++20부터) |
두 범위를 사전식 순서로 비교합니다
[
first1
,
last1
)
와
[
first2
,
last2
)
를 삼중 비교(three-way comparison)를 사용하여 비교하고, 적용 가능한 가장 강력한 비교 범주 타입의 결과를 생성합니다.
first1, last1, first2, last2, std:: compare_three_way ( ) ) ;
반환 타입이 세 가지 비교 범주 타입 중 하나가 아닌 경우, 프로그램의 형식이 올바르지 않습니다:
목차 |
매개변수
| first1, last1 | - | 검사할 첫 번째 원소들의 범위 를 정의하는 반복자 쌍 |
| first2, last2 | - | 검사할 두 번째 원소들의 범위 를 정의하는 반복자 쌍 |
| comp | - | 함수 객체 |
| 타입 요구사항 | ||
-
InputIt1, InputIt2
는
LegacyInputIterator
요구사항을 충족해야 합니다.
|
||
반환값
위에서 명시된 비교 범주 유형의 값입니다.
복잡도
N 1 를 std:: distance ( first1, last1 ) 로, N 2 를 std:: distance ( first2, last2 ) 로 정의할 때:
가능한 구현
template<class I1, class I2, class Cmp> constexpr auto lexicographical_compare_three_way(I1 f1, I1 l1, I2 f2, I2 l2, Cmp comp) -> decltype(comp(*f1, *f2)) { using ret_t = decltype(comp(*f1, *f2)); static_assert(std::disjunction_v< std::is_same<ret_t, std::strong_ordering>, std::is_same<ret_t, std::weak_ordering>, std::is_same<ret_t, std::partial_ordering>>, "반환 타입은 비교 범주 타입이어야 합니다."); bool exhaust1 = (f1 == l1); bool exhaust2 = (f2 == l2); for (; !exhaust1 && !exhaust2; exhaust1 = (++f1 == l1), exhaust2 = (++f2 == l2)) if (auto c = comp(*f1, *f2); c != 0) return c; return !exhaust1 ? std::strong_ordering::greater: !exhaust2 ? std::strong_ordering::less: std::strong_ordering::equal; } |
예제
#include <algorithm> #include <cctype> #include <compare> #include <iomanip> #include <iostream> #include <string_view> #include <utility> using namespace std::literals; void show_result(std::string_view s1, std::string_view s2, std::strong_ordering o) { std::cout << std::quoted(s1) << " is "; std::is_lt(o) ? std::cout << "less than ": std::is_gt(o) ? std::cout << "greater than ": std::cout << "equal to "; std::cout << std::quoted(s2) << '\n'; } std::strong_ordering cmp_icase(unsigned char x, unsigned char y) { return std::toupper(x) <=> std::toupper(y); }; int main() { for (const auto& [s1, s2] : { std::pair{"one"sv, "ONE"sv}, {"two"sv, "four"sv}, {"three"sv, "two"sv} }) { const auto res = std::lexicographical_compare_three_way( s1.cbegin(), s1.cend(), s2.cbegin(), s2.cend(), cmp_icase); show_result(s1, s2, res); } }
출력:
"one" is equal to "ONE" "two" is greater than "four" "three" is less than "two"
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 3410 | C++20 | 반복자 간의 불필요한 비교가 요구됨 | 해당 요구사항 제거됨 |
참고 항목
|
한 범위가 다른 범위보다 사전식으로 작으면
true
를 반환함
(함수 템플릿) |
|
|
(C++20)
|
x
<=>
y
를 구현하는 제약된 함수 객체
(클래스) |
|
(C++20)
|
한 범위가 다른 범위보다 사전식으로 작으면
true
를 반환함
(알고리즘 함수 객체) |