2011-01-06 3 views
4

내 std :: strings는 UTF-8로 인코딩되므로 std :: string < 연산자는 그것을 자르지 않습니다. 2 utf-8로 인코딩 된 std :: strings를 어떻게 비교할 수 있습니까? 그것은 악센트입니다 잘라하지 않습니다 UTF-8 문자열을 정렬 하시겠습니까?

는 é은 Z 그 후에는

감사

+1

왜 표준'영업 이익은 <' "잘라"하지 않습니다 재사용 & 붙여 넣기를 복사하기 쉽게한다 작은 래퍼있다? 주문 하는게 뭐야? –

+1

UTF-8로 인코딩 된 문자열은 동일한 UTF-32로 인코딩 된 문자열과 동일한 순서로 정렬됩니다. – dan04

+2

@Charles : 필자는 바이트 단위로 비교를 수행하고 악센트 등을 고려하지 않기 때문에 "잘라 내기"하지 않는다고 생각합니다. – Mehrdad

답변

4

당신이 UTF-8 인코딩을 정렬 무엇 인 (사전 식 순서를하지 않으려는 안 경우 제공 UTF-8로 인코딩 된 문자열을 필요에 따라 UCS-2 또는 UCS-4로 디코딩하고 원하는 비교 기능을 적용해야합니다.

점을 유지하려면 UTF-8 인코딩 메커니즘은 교묘하게 당신이 종류의 각 8 비트 인코딩 된 바이트의 숫자 값을보고, 당신은 같은 결과를 얻을 경우 먼저 디코딩 것처럼 있도록 설계 문자열을 유니 코드로 변환하고 각 코드 포인트의 숫자 값을 비교합니다.

업데이트 : 업데이트 된 질문은 순전히 사전 식 정렬보다 복잡한 비교 기능이 필요함을 나타냅니다. UTF-8 문자열을 디코드하고 디코드 된 문자를 비교해야합니다.

+0

UTF-16 인코딩은 해당 기능을 가지고 있지 않습니다. – dan04

+0

@ dan04 : 어떤 기능이 없습니까? –

+3

바이트 정렬 ANSI 스타일로 처리하지 않는 한 데이터 정렬 (정렬) 및 인코딩은 완전히 별개의 두 가지 문제입니다. http://www.joelonsoftware.com/articles/Unicode.html –

6

표준은 데이터 정렬 (정렬)과 같은 로케일 특정 항목에 대해 std::locale입니다. 환경에 LC_COLLATE=en_US.utf8 또는 이와 유사한 것이 있으면이 프로그램은 원하는대로 행을 정렬합니다. std::locale::operator()(a, b) 내가 위에 쓴 std::collate<>::compare(a, b) < 0 래퍼를 미연에 방지, 존재 내 관심을

#include <algorithm> 
#include <functional> 
#include <iostream> 
#include <iterator> 
#include <locale> 
#include <string> 
#include <vector> 
class collate_in : public std::binary_function<std::string, std::string, bool> { 
    protected: 
    const std::collate<char> &coll; 
    public: 
    collate_in(std::locale loc) 
     : coll(std::use_facet<std::collate<char> >(loc)) {} 
    bool operator()(const std::string &a, const std::string &b) const { 
     // std::collate::compare() takes C-style string (begin, end)s and 
     // returns values like strcmp or strcoll. Compare to 0 for results 
     // expected for a less<>-style comparator. 
     return coll.compare(a.c_str(), a.c_str() + a.size(), 
          b.c_str(), b.c_str() + b.size()) < 0; 
    } 
}; 
int main() { 
    std::vector<std::string> v; 
    copy(std::istream_iterator<std::string>(std::cin), 
     std::istream_iterator<std::string>(), back_inserter(v)); 
    // std::locale("") is the locale from the environment. One could also 
    // std::locale::global(std::locale("")) to set up this program's global 
    // first, and then use locale() to get the global locale, or choose a 
    // specific locale instead of using the environment's. 
    sort(v.begin(), v.end(), collate_in(std::locale(""))); 
    copy(v.begin(), v.end(), 
     std::ostream_iterator<std::string>(std::cout, "\n")); 
    return 0; 
} 
 
$ cat >file 
f 
é 
e 
d 
^D 
$ LC_COLLATE=C ./a.out file 
d 
e 
f 
é 
$ LC_COLLATE=en_US.utf8 ./a.out file 
d 
e 
é 
f 

을 가져 됐어요.

#include <algorithm> 
#include <iostream> 
#include <iterator> 
#include <locale> 
#include <string> 
#include <vector> 
int main() { 
    std::vector<std::string> v; 
    copy(std::istream_iterator<std::string>(std::cin), 
     std::istream_iterator<std::string>(), back_inserter(v)); 
    sort(v.begin(), v.end(), std::locale("")); 
    copy(v.begin(), v.end(), 
     std::ostream_iterator<std::string>(std::cout, "\n")); 
    return 0; 
} 
관련 문제