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โCharTstring_typeโstd::basic_string<CharT>
๐ ๏ธ Member functions ๐ ๏ธ
constructorโ constructs a newcollatefacet (public member function).destructorโ destructs acollatefacet (protected member function).compareโ invokesdo_compare(public member function).transformโ invokesdo_transform(public member function).hashโ invokesdo_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 ofstd::locale).collate_bynameโ represents the system-suppliedstd::collatefor 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 ยท MIT
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms