Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


Boost.Locale provides a collator class, derived from std::collate, that adds support for primary, secondary, tertiary, quaternary and identical comparison levels. They can be approximately defined as:

  1. Primary -- ignore accents and character case, comparing base letters only. For example "facade" and "Façade" are the same.
  2. Secondary -- ignore character case but consider accents. "facade" and "façade" are different but "Façade" and "façade" are the same.
  3. Tertiary -- consider both case and accents: "Façade" and "façade" are different. Ignore punctuation.
  4. Quaternary -- consider all case, accents, and punctuation. The words must be identical in terms of Unicode representation.
  5. Identical -- as quaternary, but compare code points as well.

There are two ways of using the collator facet: directly, by calling its member functions compare, transform and hash, or indirectly by using the comparator template class in STL algorithms.

For example:

    wstring a=L"Façade", b=L"facade";
    bool eq = 0 == use_facet<collator<wchar_t> >(loc).compare(collator_base::secondary,a,b);
    wcout << a <<L" and "<<b<<L" are " << (eq ? L"identical" : L"different")<<endl;

std::locale is designed to be useful as a comparison class in STL collections and algorithms. To get similar functionality with comparison levels, you must use the comparator class.

    std::map<std::string,std::string,comparator<char,collator_base::secondary> > strings;
    // Now strings uses the default system locale for string comparison

You can also set a specific locale or level when creating and using the comparator class:

    comparator<char> comp(some_locale,some_level);
    std::map<std::string,std::string,comparator<char> > strings(comp);