Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::memcmp(3)		      C++ Standard Libary		std::memcmp(3)

NAME
       std::memcmp - std::memcmp

Synopsis
	  Defined in header <cstring>
	  int memcmp( const void* lhs, const void* rhs,	std::size_t count );

	  Reinterprets	the objects pointed to by lhs and rhs as arrays	of un-
       signed char and
	  compares the first count 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 as unsigned char) that differ	in the
       objects being
	  compared.

Parameters
	  lhs, rhs - pointers to the memory buffers to compare
	  count	   - number of bytes to	examine

Return value
	  Negative value if the	first differing	 byte  (reinterpreted  as  un-
       signed char) in lhs
	  is less than the corresponding byte in rhs.

	  0 if all count bytes of lhs and rhs are equal.

	  Positive  value  if  the first differing byte	in lhs is greater than
       the corresponding
	  byte in rhs.

Notes
	  This function	reads object 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 type std::string or std::vector will
       not compare
	  their	contents, and memcmp() between two objects of type struct{char
       c; int n;} will
	  compare the padding bytes whose values may differ when the values of
       c and n are the
	  same.

Example
       // Run this code

	#include <iostream>
	#include <cstring>

	void demo(const	char* lhs, const char* rhs, std::size_t	sz)
	{
	    std::cout << std::string(lhs, sz);
	    const int rc = std::memcmp(lhs, rhs, sz);
	    if(rc < 0)
		std::cout << " precedes	";
	    else if(rc > 0)
		std::cout << " follows ";
	    else
		std::cout << " compares	equal to ";
	    std::cout  <<  std::string(rhs,  sz)  <<  "	in lexicographical or-
       der\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 order
	abd follows abc	in lexicographical order
	abc compares equal to abc in lexicographical order

See also
	  strcmp  compares two strings
		  (function)
	  strncmp compares a certain number of characters from two strings
		  (function)

http://cppreference.com		  2022.07.31			std::memcmp(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::memcmp&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help