当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ strcoll()用法及代码示例


strcoll()是内置库函数,并在头文件中声明。此函数将str1指向的字符串与str2指向的字符串进行比较。strcoll()函数根据当前语言环境的LC_COLLATE类别的规则执行比较。句法:

int strcoll(const char *str1, const char *str2)

参数:函数strcoll()将两个字符串作为参数,并返回一个整数值。

Value                   Meaning
less than zero          str1 is less than str2
zero                    str1 is equal to str2
greater than zero       str1 is greater than str2
  1. 小于零:当str1小于str2时
    // C program to illustrate strcoll() 
    #include <stdio.h> 
    #include <string.h> 
      
    int main() 
    { 
        char str1[10]; 
        char str2[10]; 
        int ret; 
      
        strcpy(str1, "geeksforgeeks"); 
        strcpy(str2, "GEEKSFORGEEKS"); 
      
        ret = strcoll(str1, str2); 
      
        if (ret > 0) { 
            printf("str1 is greater than str2"); 
        } else if (ret < 0) { 
            printf("str1 is lesser than str2"); 
        } else { 
            printf("str1 is equal to str2"); 
        } 
      
        return (0); 
    }

    输出:


    str1 is greater than str2
    
  2. 大于零:当str1大于str2时
    // C program to illustrate strcoll() 
    #include <stdio.h> 
    #include <string.h> 
      
    int main() 
    { 
        char str1[10]; 
        char str2[10]; 
        int ret; 
      
        strcpy(str1, "GEEKSFORGEEKS"); 
        strcpy(str2, "geeksforgeeks"); 
      
        ret = strcoll(str1, str2); 
      
        if (ret > 0) { 
            printf("str1 is greater than str2"); 
        } else if (ret < 0) { 
            printf("str1 is lesser than str2"); 
        } else { 
            printf("str1 is equal to str2"); 
        } 
      
        return (0); 
    }

    输出:

    str1 is lesser than str2
    
  3. 等于零:当str1等于str2时
    // C program to illustrate strcoll() 
      
    #include <stdio.h> 
    #include <string.h> 
      
    int main() 
    { 
        char str1[10]; 
        char str2[10]; 
        int ret; 
      
        strcpy(str1, "GEEKSFORGEEKS"); 
        strcpy(str2, "GEEKSFORGEEKS"); 
      
        ret = strcoll(str1, str2); 
      
        if (ret > 0) { 
            printf("str1 is greater than str2"); 
        } else if (ret < 0) { 
            printf("str1 is lesser than str2"); 
        } else { 
            printf("str1 is equal to str2"); 
        } 
      
        return (0); 
    }

    输出:

    str1 is equal to str2
    

相关函数:strcmp(),memcmp()



相关用法


注:本文由纯净天空筛选整理自 strcoll() in C/C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。