Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/byte/strcoll"

From cppreference.com
< cpp‎ | string‎ | byte
m ({{c}}, headers sorted, fmt)
m (Example: updated.)
 
Line 13: Line 13:
  
 
===Return value===
 
===Return value===
Negative value if {{c|lhs}} is ''less than'' (precedes) {{c|rhs}}.
+
* Negative value if {{c|lhs}} is ''less than'' (precedes) {{c|rhs}}.
 
+
* {{c|0}} if {{c|lhs}} is ''equal to'' {{c|rhs}}.
{{c|0}} if {{c|lhs}} is ''equal to'' {{c|rhs}}.
+
* Positive value if {{c|lhs}} is ''greater than'' (follows) {{c|rhs}}.
 
+
Positive value if {{c|lhs}} is ''greater than'' (follows) {{c|rhs}}.
+
  
 
===Notes===
 
===Notes===
Line 28: Line 26:
 
#include <cstring>
 
#include <cstring>
 
#include <iostream>
 
#include <iostream>
+
 
 
int main()
 
int main()
 
{
 
{
     std::setlocale(LC_COLLATE, "cs_CZ.iso88592");
+
     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* s1 = "hrnec";
 
     const char* s2 = "chrt";
 
     const char* s2 = "chrt";
   
+
 
 
     std::cout << "In the Czech locale: ";
 
     std::cout << "In the Czech locale: ";
 
     if (std::strcoll(s1, s2) < 0)
 
     if (std::strcoll(s1, s2) < 0)
Line 61: Line 62:
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/string/byte/strcoll]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/string/byte/strcoll]]
+
[[fr:cpp/string/byte/strcoll]]
+
[[it:cpp/string/byte/strcoll]]
+
[[ja:cpp/string/byte/strcoll]]
+
[[pt:cpp/string/byte/strcoll]]
+
[[ru:cpp/string/byte/strcoll]]
+
[[zh:cpp/string/byte/strcoll]]
+

Latest revision as of 11:48, 13 October 2023

Defined in header <cstring>
int strcoll( const char* lhs, const char* rhs );

Compares two null-terminated byte strings according to the current locale as defined by the LC_COLLATE category.

Contents

[edit] Parameters

lhs, rhs - pointers to the null-terminated byte strings to compare

[edit] Return value

  • Negative value if lhs is less than (precedes) rhs.
  • 0 if lhs is equal to rhs.
  • Positive value if lhs is greater than (follows) rhs.

[edit] Notes

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".

[edit] Example

#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

[edit] See also

compares two wide strings in accordance to the current locale
(function) [edit]
[virtual]
compares two strings using this facet's collation rules
(virtual protected member function of std::collate<CharT>) [edit]
transform a string so that strcmp would produce the same result as strcoll
(function) [edit]
C documentation for strcoll