std::strcoll(3)
π NAME π
okay so std::strcoll β this is from the C++ Standard Library (manpage std::strcoll(3)) β and it literally compares two null-terminated byte strings according to the current locale's collation rules πΈ. Basically, it's dictionary order but make it international, and it's giving main character energy for all your string-comparison needs π
β¨
β¨ SYNOPSIS β¨
Defined in header <cstring>:
int strcoll( const char* lhs, const char* rhs );
It compares two null-terminated byte strings according to the current locale as defined by the LC_COLLATE category. That's literally the whole vibe π«Ά
π PARAMETERS π
lhs,rhsβ pointers to the null-terminated byte strings you want to compare. Like, these are the two strings going head-to-head in the comparison arena π₯π
π RETURN VALUE π
- Negative value β if
lhsis less than (precedes)rhs. Solhscomes first in the dictionary, periodt. 0β iflhsis equal torhs. They're literally the same, no cap.- Positive value β if
lhsis greater than (follows)rhs. Sorhscomes first, andlhsis like "after you bestie" π
π¦ NOTES π¦
okay so collation order is literally the dictionary order. Here's the tea β:
The position of the letter in the national alphabet (its equivalence class) has higher priority than its case or variant. So like, where the letter sits in the alphabet is more important than whether it's uppercase or lowercase, or which variant it is.
Within an equivalence class, lowercase characters collate before their uppercase equivalents β so a before A, b before B, etc. β and locale-specific order may apply to characters with diacritics. (You know, the cute accented ones like Γ©, ΓΆ, Γ± β those might have their own special order depending on the locale π)
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". That's literally wild, right? Like, two or three letters are treated as one unit. Not me realizing that languages just have different vibes β and it's honestly so iconic β iykyk π
π EXAMPLE π
// Run this code
#include <clocale>
#include <cstring>
#include <iostream>
int main()
{
std::setlocale(LC_COLLATE, "cs_CZ.utf8");
// Alternatively, ISO-8859-2 (a.k.a. Latin-2)
// may also work on some OS:
// std::setlocale(LC_COLLATE, "cs_CZ.iso88592");
const char* s1 = "hrnec";
const char* s2 = "chrt";
std::cout << "In the Czech locale: ";
if (std::strcoll(s1, s2) < 0)
std::cout << s1 << " before " << s2 << '\n';
else
std::cout << s2 << " before " << s1 << '\n';
std::cout << "In lexicographical comparison: ";
if (std::strcmp(s1, s2) < 0)
std::cout << s1 << " before " << s2 << '\n';
else
std::cout << s2 << " before " << s1 << '\n';
}
β¨ OUTPUT β¨
In the Czech locale: hrnec before chrt
In lexicographical comparison: chrt before hrnec
okay so this is literally the "ch" rule from the notes in action π₯ In Czech, "ch" follows "h", so hrnec comes before chrt in the Czech locale. But lexicographical comparison says chrt comes before hrnec because c comes before h in plain character order. The difference is lowkey iconic β and <highkey>it's exactly why strcoll exists</highkey> π
β¨
π SEE ALSO π
wcscollβ compares two wide strings in accordance to the current locale (function) πdo_compareβ compares two strings using this facet's collation rules [virtual] (virtual protected member function ofstd::collate<CharT>) πstrxfrmβ transform a string so thatstrcmpwould produce the same result asstrcoll(function) π- C documentation for
strcoll
And that's the tea, bestie! π΅ This page was brought to you by http://cppreference.com, version 2024.06.10, and it's literally std::strcoll(3) β now go out there and slay those string comparisons, no cap π
β¨π¦
π¦ 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