std::collat...:do_compare(3)
π std::collat...:do_compare(3) π
okay so this is the C++ Standard Libary manual page for std::collat...:do_compare(3), and we are about to make collation make sense, bestie. no cap. π«Ά
β¨ NAME β¨
std::collate::compare,std::collate::do_compare-std::collate::compare,std::collate::do_compare
π¦ Synopsis π¦
Defined in header <locale> β¨
public:
int compare( const CharT* low1, const CharT* high1,
(1)
const CharT* low2, const CharT* high2 ) const;
protected:
virtual int do_compare( const CharT* low1, const CharT* high1,
(2)
const CharT* low2, const CharT* high2 )
const;
- Public member function, calls the protected virtual member function
do_compareof the most derived class. - Compares the character sequence
[low1, high1)to the character sequence[low2, high2), using this locale's collation rules, and returns1if the first string follows the second,-1if the first string precedes the second, zero if the two strings are equivalent.
πΈ Parameters πΈ
low1β pointer to the first character of the first stringhigh1β one past the end pointer for the first stringlow2β pointer to the first character of the second stringhigh2β one past the end pointer for the second string
π Return value π
1 if the first string is greater than the second (that is, follows the second in the collation order), -1 if the first string is less than the second (precedes the second in the collation order), zero if the two strings are equivalent. periodt. π
π₯ Notes π₯
okay so when a three-way comparison is not required (such as when providing a Compare argument to standard algorithms such as std::sort), std::locale::operator() may be more appropriate. iykyk.
Collation order is the dictionary order: the position of the letter in the national alphabet (its equivalence class) has higher priority than its case or variant. Within an equivalence class, lowercase characters collate before their uppercase equivalents and locale-specific order may apply to the characters with diacritics. In some locales, groups of characters compare as single collation units. For example, "ch" in Czech follows "h" and precedes "i", and "dzs" in Hungarian follows "dz" and precedes "g". π§
π Example π
// Run this code
#include <iostream>
#include <locale>
#include <string>
template<typename CharT>
void try_compare(const std::locale& l, const CharT* p1, const CharT* p2)
{
auto& f = std::use_facet<std::collate<CharT>>(l);
std::basic_string<CharT> s1(p1), s2(p2);
if (f.compare(&s1[0], &s1[0] + s1.size(),
&s2[0], &s2[0] + s2.size()) < 0)
std::wcout << p1 << " before " << p2 << '\n';
else
std::wcout << p2 << " before " << p1 << '\n';
}
int main()
{
std::locale::global(std::locale("en_US.utf8"));
std::wcout.imbue(std::locale());
std::wcout << "In the American locale: ";
try_compare(std::locale(), "hrnec", "chrt");
std::wcout << "In the Czech locale: ";
try_compare(std::locale("cs_CZ.utf8"), "hrnec", "chrt");
std::wcout << "In the American locale: ";
try_compare(std::locale(), L"Γ₯r", L"Γ€ngel");
std::wcout << "In the Swedish locale: ";
try_compare(std::locale("sv_SE.utf8"), L"Γ₯r", L"Γ€ngel");
}
π Output π
In the American locale: chrt before hrnec
In the Czech locale: hrnec before chrt
In the American locale: Γ€ngel before Γ₯r
In the Swedish locale: Γ₯r before Γ€ngel
π See also π
strcollβ compares two strings in accordance to the current locale (function)wcscollβ compares two wide strings in accordance to the current locale (function)operator()β lexicographically compares two strings using this locale's collate facet (public member function ofstd::locale)
π sign-off: source/version http://cppreference.com, date 2024.06.10, page std::collat...:do_compare(3) β bye bestie, go collate something iconic! π
π¦ 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