ranges::lexicographical_compare
ranges::lexicographical_compare (사전식 비교 — ranges)
두 범위를 사전식으로 비교해 어느 쪽이 작은지 반환하는 ranges 버전 알고리즘이에요. <algorithm> 헤더에 있어요.
출처: cppreference
본문
std::ranges::lexicographical_compare는 두 범위를 사전 순서로 비교해요.
namespace std::ranges {
template< std::input_iterator I1, std::sentinel_for<I1> S1,
std::input_iterator I2, std::sentinel_for<I2> S2,
class Comp = ranges::less,
class Proj1 = std::identity, class Proj2 = std::identity >
constexpr bool lexicographical_compare( I1 first1, S1 last1,
I2 first2, S2 last2,
Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
}
- 첫 번째로 달라지는 원소에서 작은 쪽이 이기고, 접두사면 짧은 쪽이 작아요.
- 반환 값: 첫 범위가 사전식으로 작으면
true.
std::string a = "apple";
std::string b = "banana";
bool less = std::ranges::lexicographical_compare(a, b); // true
임의 범위의 가나다순 비교가 필요할 때 쓰는 ranges 버전이에요.