Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/wide/wmemcmp"

From cppreference.com
< cpp‎ | string‎ | wide
m (typo)
m ({{c}}, headers sorted, fmt, langlinks)
 
Line 1: Line 1:
 
{{cpp/title|wmemcmp}}
 
{{cpp/title|wmemcmp}}
 
{{cpp/string/wide/navbar}}
 
{{cpp/string/wide/navbar}}
{{ddcl | header=cwchar |
+
{{ddcl|header=cwchar|
 
int wmemcmp( const wchar_t* lhs, const wchar_t* rhs, std::size_t count );
 
int wmemcmp( const wchar_t* lhs, const wchar_t* rhs, std::size_t count );
 
}}
 
}}
  
Compares the first {{tt|count}} wide characters of the wide character arrays pointed to by {{tt|lhs}} and {{tt|rhs}}. The comparison is done lexicographically.
+
Compares the first {{c|count}} wide characters of the wide character arrays pointed to by {{c|lhs}} and {{c|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.
 
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 {{tt|count}} is zero, the function does nothing.
+
If {{c|count}} is zero, the function does nothing.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | lhs, rhs | pointers to the wide character arrays to compare}}
+
{{par|lhs, rhs|pointers to the wide character arrays to compare}}
{{par | count | number of wide characters to examine}}
+
{{par|count|number of wide characters to examine}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
Negative value if the value of the first differing wide character in {{tt|lhs}} is less than the value of the corresponding wide character in {{tt|rhs}}: {{tt|lhs}} precedes {{tt|rhs}} in lexicographical order.
+
Negative value if the value of the first differing wide character in {{c|lhs}} is less than the value of the corresponding wide character in {{c|rhs}}: {{c|lhs}} precedes {{c|rhs}} in lexicographical order.
  
{{c|0}} if all {{tt|count}} wide characters of {{tt|lhs}} and {{tt|rhs}} are equal.
+
{{c|0}} if all {{c|count}} wide characters of {{c|lhs}} and {{c|rhs}} are equal.
  
Positive value if the value of the first differing wide character in {{tt|lhs}} is greater than the value of the corresponding wide character in {{tt|rhs}}: {{tt|rhs}} precedes {{tt|lhs}} in lexicographical order.
+
Positive value if the value of the first differing wide character in {{c|lhs}} is greater than the value of the corresponding wide character in {{c|rhs}}: {{c|rhs}} precedes {{c|lhs}} in lexicographical order.
  
 
===Notes===
 
===Notes===
Line 29: Line 29:
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <clocale>
#include <iostream>
+
#include <string>
+
 
#include <cwchar>
 
#include <cwchar>
 +
#include <iostream>
 
#include <locale>
 
#include <locale>
#include <clocale>
+
#include <string>
  
 
void demo(const wchar_t* lhs, const wchar_t* rhs, std::size_t sz)
 
void demo(const wchar_t* lhs, const wchar_t* rhs, std::size_t sz)
Line 41: Line 40:
 
     std::wcout << std::wstring(lhs, sz);
 
     std::wcout << std::wstring(lhs, sz);
 
     int rc = std::wmemcmp(lhs, rhs, sz);
 
     int rc = std::wmemcmp(lhs, rhs, sz);
     if(rc == 0)
+
     if (rc == 0)
 
         std::wcout << " compares equal to ";
 
         std::wcout << " compares equal to ";
     else if(rc < 0)
+
     else if (rc < 0)
 
         std::wcout << " precedes ";
 
         std::wcout << " precedes ";
     else if(rc > 0)
+
     else if (rc > 0)
 
         std::wcout << " follows ";
 
         std::wcout << " follows ";
 
     std::wcout << std::wstring(rhs, sz) << " in lexicographical order\n";
 
     std::wcout << std::wstring(rhs, sz) << " in lexicographical order\n";
Line 64: Line 63:
 
}
 
}
 
|p=true
 
|p=true
| output=
+
|output=
 
αβγ precedes αβδ in lexicographical order
 
αβγ precedes αβδ in lexicographical order
 
αβδ follows αβγ in lexicographical order
 
αβδ follows αβγ in lexicographical order
Line 72: Line 71:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/wide/dsc wcscmp}}
+
{{dsc inc|cpp/string/wide/dsc wcscmp}}
{{dsc inc | cpp/string/byte/dsc memcmp}}
+
{{dsc inc|cpp/string/byte/dsc memcmp}}
{{dsc inc | cpp/string/wide/dsc wcsncmp}}
+
{{dsc inc|cpp/string/wide/dsc wcsncmp}}
{{dsc see c | c/string/wide/wmemcmp}}
+
{{dsc see c|c/string/wide/wmemcmp}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/string/wide/wmemcmp]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/string/wide/wmemcmp]]
+
[[fr:cpp/string/wide/wmemcmp]]
+
[[it:cpp/string/wide/wmemcmp]]
+
[[ja:cpp/string/wide/wmemcmp]]
+
[[pt:cpp/string/wide/wmemcmp]]
+
[[ru:cpp/string/wide/wmemcmp]]
+
[[zh:cpp/string/wide/wmemcmp]]
+

Latest revision as of 11:14, 10 June 2023

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

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.

Contents

[edit] Parameters

lhs, rhs - pointers to the wide character arrays to compare
count - number of wide characters to examine

[edit] 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.

[edit] 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.

[edit] Example

#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

[edit] See also

compares two wide strings
(function) [edit]
compares two buffers
(function) [edit]
compares a certain amount of characters from two wide strings
(function) [edit]
C documentation for wmemcmp