Namespaces
Variants

std::collate<CharT>:: compare, std::collate<CharT>:: do_compare

From cppreference.net
헤더 파일에 정의됨 <locale>
public :

int compare ( const CharT * low1, const CharT * high1,

const CharT * low2, const CharT * high2 ) const ;
(1)
protected :

virtual int do_compare ( const CharT * low1, const CharT * high1,

const CharT * low2, const CharT * high2 ) const ;
(2)
1) Public 멤버 함수로서, 가장 파생된 클래스의 protected virtual 멤버 함수 do_compare 를 호출합니다.
2) 문자 시퀀스 [ low1 , high1 ) 를 문자 시퀀스 [ low2 , high2 ) 와 비교하여, 이 로캘의 문자열 정렬 규칙을 사용하고, 첫 번째 문자열이 두 번째 문자열보다 뒤에 오면 1 을 반환하고, 첫 번째 문자열이 두 번째 문자열보다 앞에 오면 - 1 을 반환하며, 두 문자열이 동등하면 0을 반환합니다.

목차

매개변수

low1 - 첫 번째 문자열의 첫 번째 문자를 가리키는 포인터
high1 - 첫 번째 문자열의 끝 바로 다음을 가리키는 포인터
low2 - 두 번째 문자열의 첫 번째 문자를 가리키는 포인터
high2 - 두 번째 문자열의 끝 바로 다음을 가리키는 포인터

반환값

1 첫 번째 문자열이 두 번째 문자열보다 큰 경우(즉, 정렬 순서에서 두 번째 문자열을 따르는 경우), - 1 첫 번째 문자열이 두 번째 문자열보다 작은 경우(정렬 순서에서 두 번째 문자열 앞에 오는 경우), 두 문자열이 동등한 경우 0을 반환합니다.

참고 사항

3방향 비교가 필요하지 않은 경우(예: std::sort 와 같은 표준 알고리즘에 Compare 인수를 제공할 때), std::locale::operator() 를 사용하는 것이 더 적합할 수 있습니다.

정렬 순서는 사전 순서입니다: 국가별 알파벳에서 문자의 위치(그것의 동등 클래스 )는 대소문자나 변형보다 우선순위를 가집니다. 동등 클래스 내에서는 소문자가 해당 대문자 등가물보다 먼저 정렬되며, 발음 구별 기호가 있는 문자에는 로캘별 순서가 적용될 수 있습니다. 일부 로캘에서는 문자 그룹이 단일 정렬 단위 로 비교됩니다. 예를 들어, "ch" 는 체코어에서 "h" 다음에 오고 "i" 앞에 옵니다. 또한 "dzs" 는 헝가리어에서 "dz" 다음에 오고 "g" 앞에 옵니다.

예제

#include <iostream>
#include <locale>
#include <string>
template<typename CharT>
void try_compare(const std::locale& l, const CharT* p1, const CharT* p2)
{
    auto& f = std::use_facet<std::collate<CharT>>(l);
    std::basic_string<CharT> s1(p1), s2(p2);
    if (f.compare(&s1[0], &s1[0] + s1.size(),
                  &s2[0], &s2[0] + s2.size()) < 0)
        std::wcout << p1 << " before " << p2 << '\n';
    else
        std::wcout << p2 << " before " << p1 << '\n';
}
int main()
{
    std::locale::global(std::locale("en_US.utf8"));
    std::wcout.imbue(std::locale());
    std::wcout << "In the American locale: ";
    try_compare(std::locale(), "hrnec", "chrt");
    std::wcout << "In the Czech locale: ";
    try_compare(std::locale("cs_CZ.utf8"), "hrnec", "chrt");
    std::wcout << "In the American locale: ";
    try_compare(std::locale(), L"år", L"ängel");
    std::wcout << "In the Swedish locale: ";
    try_compare(std::locale("sv_SE.utf8"), L"år", L"ängel");
}

출력:

In the American locale: chrt before hrnec
In the Czech locale: hrnec before chrt
In the American locale: ängel before år
In the Swedish locale: år before ängel

참고 항목

현재 로캘에 따라 두 문자열을 비교합니다
(함수)
현재 로캘에 따라 두 와이드 문자열을 비교합니다
(함수)
이 로캘의 collate 패싯을 사용하여 두 문자열을 사전식으로 비교합니다
( std::locale 의 public 멤버 함수)