Namespaces
Variants
Views
Actions

std::strcoll

From cppreference.com
< cpp‎ | string‎ | byte
Revision as of 09:05, 30 December 2012 by Cubbi (Talk | contribs)

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

Parameters

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

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.

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

Example

#include <iostream>
#include <cstring>
#include <clocale>
 
int main()
{
    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

See also

Template:cpp/string/wide/dcl list wcscollTemplate:cpp/locale/collate/dcl list do compareTemplate:cpp/string/byte/dcl list strxfrm
C documentation for strcoll