std::collate(3)

okay so this is the std::collate(3) page from the C++ Standard Libary manual, and bestie we are about to make locale collation make sense ๐Ÿ’…โœจ

๐Ÿ’– NAME ๐Ÿ’–

std::collate - std::collate โ€” yes, thatโ€™s literally the NAME line, itโ€™s giving minimal and mysterious, periodt.

โœจ Synopsis โœจ

Defined in header <locale>
template< class CharT >
class collate;

okay so std::collate is the class that wraps up locale-specific collation (thatโ€™s comparison, bestie) 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. iconic behavior, ngl.

thereโ€™s also a little diagram situation: std-collate-inheritance.svg โ€” the Inheritance diagram ๐Ÿฆ‹

๐ŸŒ€ Specializations ๐ŸŒ€

The standard library is guaranteed to provide the following specializations โ€” and any locale object is required to implement them, no cap:

Defined in header <locale>

  • std::collate<char> โ€” implements lexicographical ordering of byte strings.
  • std::collate<wchar_t> โ€” implements lexicographical ordering of wide strings.

๐Ÿงธ Member types ๐Ÿงธ

  • char_type โ€” CharT
  • string_type โ€” std::basic_string<CharT>

๐Ÿ› ๏ธ Member functions ๐Ÿ› ๏ธ

  • constructor โ€” constructs a new collate facet (public member function).
  • destructor โ€” destructs a collate facet (protected member function).
  • compare โ€” invokes do_compare (public member function).
  • transform โ€” invokes do_transform (public member function).
  • hash โ€” invokes do_hash (public member function).

๐ŸŽ Member objects ๐ŸŽ

  • static std::locale::id id โ€” id of the locale (public member object).

๐Ÿ” Protected member functions ๐Ÿ”

  • do_compare [virtual] โ€” compares two strings using this facet's collation rules (virtual protected member function).
  • do_transform [virtual] โ€” transforms a string so that collation can be replaced by comparison (virtual protected member function).
  • do_hash [virtual] โ€” generates an integer hash value using this facet's collation rules (virtual protected member function).

๐ŸŒŸ Example ๐ŸŒŸ

// Run this code

#include <algorithm>
#include <iostream>
#include <locale>
#include <string>
#include <vector>

int main()
{
    std::locale::global(std::locale("en_US.utf8"));
    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 ๐Ÿ‘€

  • operator() โ€” lexicographically compares two strings using this locale's collate facet (public member function of std::locale).
  • collate_byname โ€” represents the system-supplied std::collate for the named locale (class template).

source/version/date check, bestie: http://cppreference.com, 2024.06.10, page std::collate(3) โ€” mwah, love u, bye ๐Ÿ’‹๐Ÿซถ

๐Ÿ“ฆ where this page comes from