📜  C++中的std :: memcmp()

📅  最后修改于: 2021-05-30 02:59:31             🧑  作者: Mango

它比较buf1和buf2指向的数组的第一个计数字符。
句法:

int memcmp(const void *buf1, const void *buf2, size_t count);
Return Value: it returns an integer.
Parameters:  
buf1 : Pointer to block of memory.
buf2 : Pointer to block of memory.
count : Maximum numbers of bytes to compare.
Return Value is interpreted as :
Value                                Meaning
Less than zero                       buf1 is less than buf2.
Zero                                 buf1 is equal to buf2.
Greater than zero                    buf1 is greater than buf2.

例子1.当计数大于零(> 0)

// CPP program to illustrate std::memcmp()
#include 
#include 
  
int main()
{
    char buff1[] = "Welcome to GeeksforGeeks";
    char buff2[] = "Hello Geeks ";
  
    int a;
  
    a = std::memcmp(buff1, buff2, sizeof(buff1));
  
    if (a > 0)
        std::cout << buff1 << " is greater than " << buff2;
    else if (a < 0)
        std::cout << buff1 << "is less than " << buff2;
    else
        std::cout << buff1 << " is the same as " << buff2;
  
    return 0;
}

输出:

Welcome to GeeksforGeeks is greater than Hello Geeks 

例子2.当计数小于零(<0)

// CPP program to illustrate std::memcmp() 
#include 
#include 
  
int main()
{
    int comp = memcmp("GEEKSFORGEEKS", "geeksforgeeks", 6);
    if (comp == 0) {
        std::cout << "both are equal";
    }
    if (comp < 0) {
        std::cout << "String 1  is less than String 2";
    } else {
        std::cout << "String 1 is  greater than String 2";
    }
}

输出:

String 1 is less than String 2

示例3:当计数等于零(= 0)时

// CPP program to illustrate std::memcmp()
#include 
#include 
  
int main()
{
    char buff1[] = "Welcome to GeeksforGeeks";
    char buff2[] = "Welcome to GeeksforGeeks";
  
    int a;
  
    a = std::memcmp(buff1, buff2, sizeof(buff1));
  
    if (a > 0)
        std::cout << buff1 << " is greater than " << buff2;
    else if (a < 0)
        std::cout << buff1 << "is less than " << buff2;
    else
        std::cout << buff1 << " is the same as " << buff2;
  
    return 0;
}

输出:

Welcome to GeeksforGeeks is the same as Welcome to GeeksforGeeks
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”