命名空间
变体
操作

std::wmemcmp

来自 cppreference.cn
< cpp‎ | string‎ | wide
在头文件 <cwchar> 中定义
int wmemcmp( const wchar_t* lhs, const wchar_t* rhs, std::size_t count );

比较 lhsrhs 指向的宽字符数组的前 count 个宽字符。比较按字典序进行。

结果的符号是所比较数组中第一个不同宽字符对的值之间的差的符号。

如果 count 为零,函数不执行任何操作。

目录

[编辑] 参数

lhs, rhs - 指向要比较的宽字符数组的指针
count - 要检查的宽字符数

[编辑] 返回值

如果 lhs 中第一个不同宽字符的值小于 rhs 中相应宽字符的值,则返回负值:lhs 在字典序上优先于 rhs

如果 lhsrhs 的所有 count 个宽字符都相等,则返回 0

如果 lhs 中第一个不同宽字符的值大于 rhs 中相应宽字符的值,则返回正值:rhs 在字典序上优先于 lhs

[编辑] 注意

此函数不区分区域设置,也不关心它检查的 wchar_t 对象的值:空字符和无效宽字符也会被比较。

[编辑] 示例

#include <clocale>
#include <cwchar>
#include <iostream>
#include <locale>
#include <string>
 
void demo(const wchar_t* lhs, const wchar_t* rhs, std::size_t sz)
{
    std::wcout << std::wstring(lhs, sz);
    int rc = std::wmemcmp(lhs, rhs, sz);
    if (rc == 0)
        std::wcout << " compares equal to ";
    else if (rc < 0)
        std::wcout << " precedes ";
    else if (rc > 0)
        std::wcout << " follows ";
    std::wcout << std::wstring(rhs, sz) << " in lexicographical order\n";
}
 
int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
    std::wcout.imbue(std::locale("en_US.utf8"));
 
    wchar_t a1[] = {L'α',L'β',L'γ'};
    constexpr std::size_t sz = sizeof a1 / sizeof *a1;
    wchar_t a2[sz] = {L'α',L'β',L'δ'};
 
    demo(a1, a2, sz);
    demo(a2, a1, sz);
    demo(a1, a1, sz);
}

可能的输出

αβγ precedes αβδ in lexicographical order
αβδ follows αβγ in lexicographical order
αβγ compares equal to αβγ in lexicographical order

[编辑] 参阅

比较两个宽字符串
(函数) [编辑]
比较两个缓冲区
(函数) [编辑]
比较两个宽字符串中的一定数量的字符
(函数) [编辑]
C 文档 用于 wmemcmp
English 日本語 中文(简体) 中文(繁體)