📜  C++ strcoll()

📅  最后修改于: 2020-09-25 09:08:43             🧑  作者: Mango

C++中的strcoll() 函数比较两个null终止字符串。比较基于LC_COLLATE类别定义的当前语言环境。

strcmp()对于大多数字符串比较就足够了,但是在处理unicode 字符,有时有些细微差别会使字节到字节的字符串比较不正确。

例如,如果您要比较两个西班牙语字符串 ,则它们可以包含加重字符,如á,é,í,ó,ú,ü,ñ,¿,¡等。

默认情况下,这样的突出字符来A,B,C … Z的字母全后。这种比较是错误的,因为a的不同重音实际上应该在b之前。

在这种情况下,strcoll()使用当前语言环境执行比较,从而提供更准确的结果。

它在头文件中定义。

strcoll()原型

int strcoll( const char* lhs, const char* rhs );

strcoll() 函数采用两个参数: lhsrhs 。它根据LC_COLLATE类别的当前语言环境比较lhsrhs的内容。

strcoll()参数

strcoll()返回值

strcoll() 函数返回:

示例:strcoll() 函数如何工作?

#include 
#include 
using namespace std;

int main()
{
    char lhs[] = "Armstrong";
    char rhs[] = "Army";
    int result;
    result = strcoll(lhs,rhs);

    cout << "In the current locale ";
    if(result > 0)
        cout << rhs << " precedes " << lhs << endl;
    else if (result < 0)
        cout << lhs << " precedes " << rhs << endl;
    else
        cout << lhs << " and " << rhs << " are same" << endl;
        
    return 0;
}

运行该程序时,输出为:

In the current locale Armstrong precedes Army