首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::wcsncmp

Defined in header <cwchar>

int wcsncmp( const wchar_t* lhs, const wchar_t* rhs, std::size_t count );

最多比较count两个以空结尾的宽字符串的宽字符。比较是按字典顺序进行的。

结果的符号是所比较的字符串中第一对宽字符的值之间差异的符号。

如果lhsrhs不是指向以空结尾的字符串的指针。

参数

lhs, rhs

-

pointers to the null-terminated wide strings to compare

count

-

maximum number of characters to compare

返回值

负值lhs出现在前面rhs按字典顺序排列。

零中频lhsrhs比较平等。

正值lhs出现在rhs按字典顺序排列。

二次

代码语言:javascript
复制
#include <iostream>
#include <cwchar>
#include <clocale>
#include <locale>
 
void demo(const wchar_t* lhs, const wchar_t* rhs, int sz)
{
    int rc = std::wcsncmp(lhs, rhs, sz);
    if(rc == 0)
        std::wcout << "First " << sz << " characters of ["
                  << lhs << "] equal [" << rhs << "]\n";
    else if(rc < 0)
        std::wcout << "First " << sz << " characters of ["
                  << lhs << "] precede [" << rhs << "]\n";
    else if(rc > 0)
        std::wcout << "First " << sz << " characters of ["
                  << lhs << "] follow [" << rhs << "]\n";
}
 
int main()
{
    const wchar_t str1[] = L"안녕하세요";
    const wchar_t str2[] = L"안녕히 가십시오";
 
    std::setlocale(LC_ALL, "en_US.utf8");
    std::wcout.imbue(std::locale("en_US.utf8"));
    demo(str1, str2, 5);
    demo(str2, str1, 8);
    demo(str1, str2, 2);
}

二次

产出:

二次

代码语言:javascript
复制
First 5 characters of [안녕하세요] precede [안녕히 가십시오]
First 8 characters of [안녕히 가십시오] follow [안녕하세요]
First 2 characters of [안녕하세요] equal [안녕히 가십시오]

二次

另见

strncmp

compares a certain amount of characters of two strings (function)

wcscmp

compares two wide strings (function)

wmemcmp

compares a certain amount of wide characters from two arrays (function)

wcscoll

compares two wide strings in accordance to the current locale (function)

c wcsncmp文档

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券