std::wmemcmp(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::wmemcmp(3) β€” C++ Standard Libary edition πŸ’–

Okay bestie, welcome to the std::wmemcmp page β€” yes, the manual name is literally C++ Standard Libary up top, spelling and all πŸ˜­πŸ’…. This is the wide-character comparison function you need to know.


🌸 NAME 🌸

std::wmemcmp β€” std::wmemcmp (iconic, no notes, periodt πŸ’…βœ¨)

🧚 Synopsis 🧚

Defined in header <cwchar>
int wmemcmp( const wchar_t* lhs, const wchar_t* rhs, std::size_t count );

It compares the first count wide characters of the wide character arrays pointed to by lhs and rhs. The comparison is done lexicographically.

The sign of the result is the sign of the difference between the values of the first pair of wide characters that differ in the arrays being compared.

If count is zero, the function does nothing. Literally nothing, bestie. 🫢

πŸ¦‹ Parameters πŸ¦‹

  • lhs, rhs β€” pointers to the wide character arrays to compare.
  • count β€” number of wide characters to examine.

πŸ‘‘ Return value πŸ‘‘

  • Negative value if the value of the first differing wide character in lhs is less than the value of the corresponding wide character in rhs: lhs precedes rhs in lexicographical order.
  • 0 if all count wide characters of lhs and rhs are equal.
  • Positive value if the value of the first differing wide character in lhs is greater than the value of the corresponding wide character in rhs: rhs precedes lhs in lexicographical order.

πŸ’€ Notes πŸ’€

This function is not locale-sensitive and pays no attention to the values of the wchar_t objects it examines: nulls as well as invalid wide characters are compared too. It's giving equal-opportunity wide-character comparison, no cap. ✨

πŸ”₯ Example πŸ”₯

// Run this code

#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);
}

🌈 Possible output 🌈

Ξ±Ξ²Ξ³ precedes Ξ±Ξ²Ξ΄ in lexicographical order
Ξ±Ξ²Ξ΄ follows Ξ±Ξ²Ξ³ in lexicographical order
Ξ±Ξ²Ξ³ compares equal to Ξ±Ξ²Ξ³ in lexicographical order

🌟 See also 🌟

  • wcscmp β€” compares two wide strings (function).
  • memcmp β€” compares two buffers (function).
  • wcsncmp β€” compares a certain amount of characters from two wide strings (function).
  • C documentation for wmemcmp.

source/version/date tea β˜•: this little page comes from http://cppreference.com, dated 2024.06.10, and it's serving std::wmemcmp(3) realness. mwah πŸ’‹πŸ’–

πŸ“¦ where this page comes from