Localization library | |||||||||||||||||||||||||
Regular expressions library(C++11) | |||||||||||||||||||||||||
Formatting library(C++20) | |||||||||||||||||||||||||
Null-terminated sequence utilities | |||||||||||||||||||||||||
Byte strings | |||||||||||||||||||||||||
Multibyte strings | |||||||||||||||||||||||||
Wide strings | |||||||||||||||||||||||||
Primitive numeric conversions | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
Text encoding identifications | |||||||||||||||||||||||||
|
Functions | ||||||||||||||||||||||||||||||||||||
Character classification | ||||||||||||||||||||||||||||||||||||
Character manipulation | ||||||||||||||||||||||||||||||||||||
Conversions to numeric formats | ||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||
String manipulation | ||||||||||||||||||||||||||||||||||||
String examination | ||||||||||||||||||||||||||||||||||||
Character array functions | ||||||||||||||||||||||||||||||||||||
Miscellaneous | ||||||||||||||||||||||||||||||||||||
Defined in header <cstring> | ||
int memcmp(constvoid* lhs,constvoid* rhs,std::size_t count); | ||
Reinterprets the objects pointed to bylhs andrhs as arrays ofunsignedchar and compares the firstcount bytes of these arrays. The comparison is done lexicographically.
The sign of the result is the sign of the difference between the values of the first pair of bytes (both interpreted asunsignedchar) that differ in the objects being compared.
Contents |
lhs, rhs | - | pointers to the memory buffers to compare |
count | - | number of bytes to examine |
Negative value if the first differing byte (reinterpreted asunsignedchar) inlhs is less than the corresponding byte inrhs.
0 if allcount bytes oflhs andrhs are equal.
Positive value if the first differing byte inlhs is greater than the corresponding byte inrhs.
This function readsobject representations, not the object values, and is typically meaningful for only trivially-copyable objects that have no padding. For example,memcmp()
between two objects of typestd::string orstd::vector will not compare their contents,memcmp()
between two objects of typestruct{char c;int n;} will compare the padding bytes whose values may differ when the values ofc andn are the same, and even if there were no padding bytes, theint
would be compared without taking into account endianness.
#include <cstring>#include <iostream> void demo(constchar* lhs,constchar* rhs,std::size_t sz){std::cout<<std::string(lhs, sz);constint rc= std::memcmp(lhs, rhs, sz);if(rc<0)std::cout<<" precedes ";elseif(rc>0)std::cout<<" follows ";elsestd::cout<<" compares equal to ";std::cout<<std::string(rhs, sz)<<" in lexicographical order\n";} int main(){char a1[]={'a','b','c'};char a2[sizeof a1]={'a','b','d'}; demo(a1, a2, sizeof a1); demo(a2, a1, sizeof a1); demo(a1, a1, sizeof a1);}
Output:
abc precedes abd in lexicographical orderabd follows abc in lexicographical orderabc compares equal to abc in lexicographical order
compares two strings (function)[edit] | |
compares a certain number of characters from two strings (function)[edit] | |
C documentation formemcmp |