Namespaces
Variants
Views
Actions

std::collate

From cppreference.com
< cpp‎ | locale
Revision as of 20:34, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <locale>
</td>

<td></td> <td></td> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td class="t-dcl-nopad">
template< class CharT >
class collate;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Class std::collate encapsulates locale-specific collation (comparison) and hashing of strings. This facet is used by std::basic_regex and can be applied, by means of std::locale::operator(), directly to all standard algorithms that expect a string comparison predicate.

cpp/locale/locale/facetstd-collate-inheritance.svg
About this image

Inheritance diagram

Two specializations are provided by the standard library and are implemented by all locale objects created in a C++ program:

Defined in header <locale>
std::collate<char> implements lexicographical ordering of byte strings
std::collate<wchar_t> implements lexicographical ordering of wide strings

Contents

Member types

Member type Definition
char_type charT
string_type std::basic_string<charT>

Member functions

Template:cpp/locale/collate/dcl list compareTemplate:cpp/locale/collate/dcl list transformTemplate:cpp/locale/collate/dcl list hash
constructs a new collate facet
(public member function)
destructs a collate facet
(protected member function)

Member objects

static std::locale::id id
id of the locale
(public member object)

Protected member functions

Template:cpp/locale/collate/dcl list do compareTemplate:cpp/locale/collate/dcl list do transformTemplate:cpp/locale/collate/dcl list do hash

Example

#include <locale>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
    std::wcout.imbue(std::locale(""));
    std::vector<std::wstring> v = {L"ar", L"zebra", L"\u00f6grupp", L"Zebra", L"\u00e4ngel",
                                   L"\u00e5r", L"f\u00f6rnamn"};
 
    std::wcout << "Default locale collation order: ";
    std::sort(v.begin(), v.end());
    for(auto s : v) std::wcout << s << ' '; std::wcout << '\n';
 
    std::wcout << "English locale collation order: ";
    std::sort(v.begin(), v.end(), std::locale("en_US.UTF-8"));
    for(auto s : v) std::wcout << s << ' '; std::wcout << '\n';
 
    std::wcout << "Swedish locale collation order: ";
    std::sort(v.begin(), v.end(), std::locale("sv_SE.UTF-8"));
    for(auto s : v) std::wcout << s << ' '; std::wcout << '\n';
}

Output:

Default locale collation order: Zebra ar förnamn zebra ängel år ögrupp
English locale collation order: ängel ar år förnamn ögrupp zebra Zebra
Swedish locale collation order: ar förnamn zebra Zebra år ängel ögrupp

See also

Template:cpp/locale/locale/dcl list operator()
creates a collate facet for the named locale
(class template)