[ Web Proxy ]
URL:
Viewing: http://ja.cppreference.com/cpp/algorithm/lexicographical_compare [Back]  [Original]

std::lexicographical_compare - cppreference.com
cppreference.com

std::lexicographical_compare

: cppreference.com
 
C++
(C++20)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
 
(C++20)
: std::sortable, std::projected, ...
: std::ranges::copy, std::ranges::sort, ...
(C++17)
(C++11)(C++11)(C++11)
(C++17)
(C++11)
()
(C++11)
/
(C++11)
(C++17)

C
 
<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>
(1)
template< class InputIt1, class InputIt2 > bool lexicographical_compare( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 );
(C++20)
template< class InputIt1, class InputIt2 > constexpr bool lexicographical_compare( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 );
(C++20)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > bool lexicographical_compare( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2 );
(2) (C++17)
(3)
template< class InputIt1, class InputIt2, class Compare > bool lexicographical_compare( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp );
(C++20)
template< class InputIt1, class InputIt2, class Compare > constexpr bool lexicographical_compare( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp );
(C++20)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class Compare > bool lexicographical_compare( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, Compare comp );
(4) (C++17)

1 [first1, last1) 2 [first2, last2)

1) operator<
3) comp
2,4) (1,3) policy std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> true

  • 2
  • 2
  • 2

first1, last1 - 1
first2, last2 - 2
policy -
comp - 12 true (Compare )

bool cmp(const Type1 &a, const Type2 &b);

const & Type1 Type2 ( const ) ( Type1 & Type1 Type1 (C++11))
Type1 Type2 InputIt1 InputIt2 Type1 Type2

-
InputIt1, InputIt2 LegacyInputIterator
-
ForwardIt1, ForwardIt2 LegacyForwardIterator

12 true

2·min(N1, N2) N1 = std::distance(first1, last1) N2 = std::distance(first2, last2)

ExecutionPolicy

1
template<class InputIt1, class InputIt2>
bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
                             InputIt2 first2, InputIt2 last2)
{
    for ( ; (first1 != last1) && (first2 != last2); ++first1, (void) ++first2 ) {
        if (*first1 < *first2) return true;
        if (*first2 < *first1) return false;
    }
    return (first1 == last1) && (first2 != last2);
}
2
template<class InputIt1, class InputIt2, class Compare>
bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
                             InputIt2 first2, InputIt2 last2,
                             Compare comp)
{
    for ( ; (first1 != last1) && (first2 != last2); ++first1, (void) ++first2 ) {
        if (comp(*first1, *first2)) return true;
        if (comp(*first2, *first1)) return false;
    }
    return (first1 == last1) && (first2 != last2);
}

#include <algorithm>
#include <iostream>
#include <vector>
#include <random>

int main()
{
    std::vector<char> v1 {'a', 'b', 'c', 'd'};
    std::vector<char> v2 {'a', 'b', 'c', 'd'};
 
    std::mt19937 g{std::random_device{}()};
    while (!std::lexicographical_compare(v1.begin(), v1.end(),
                                         v2.begin(), v2.end())) {
        for (auto c : v1) std::cout << c << ' ';
        std::cout << ">= ";
        for (auto c : v2) std::cout << c << ' ';
        std::cout << '\n';
 
        std::shuffle(v1.begin(), v1.end(), g);
        std::shuffle(v2.begin(), v2.end(), g);
    }
 
    for (auto c : v1) std::cout << c << ' ';
    std::cout << "< ";
    for (auto c : v2) std::cout << c << ' ';
    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

2
() [edit]

Web Proxy Viewer  |  New URL  |  Original Page