std::collate
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Text processing library
Regular expressions library (C++11)
Formatting library (C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++26)
Localization library
collate
(C++11/17/26*)
(C++11/17/26*)
(C++11/17/26*)
(C++11/17/26*)
(C++11/17/26*)
(C++11/17/26*)
std::collate
Defined in header
<locale>
template< class CharT >
class collate;
class collate;
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.
std-collate-inheritance.svg
Inheritance diagram
Contents
[edit] Specializations
The standard library is guaranteed to provide the following specializations (they are required to be implemented by any locale object):
Defined in header
<locale>
std::collate<char>
implements lexicographical ordering of byte strings
std::collate<wchar_t>
implements lexicographical ordering of wide strings
[edit] Nested types
Type
Definition
char_type
CharT
string_type
std::basic_string <CharT>
[edit] Data members
Member
Description
[edit] Member functions
[edit] Protected member functions
[virtual]
(virtual protected member function) [edit]
[virtual]
(virtual protected member function) [edit]
[virtual]
(virtual protected member function) [edit]
[edit] 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
[edit] See also
lexicographically compares two strings using this locale's collate facet
(public member function of
(public member function of
std::locale
) [edit]