比較兩個int型陣列的大小

2021-06-22 10:26:34 字數 843 閱讀 8133

一般大家都知道比較字元型陣列大小的函式strcmp

下面是strcmp的介紹:

原型:extern int strcmp(const char *s1,const char * s2);

所在標頭檔案:

string.h

功能:比較字串s1和s2。

一般形式:strcmp(字串1,字串2)

說明:當s1當s1=s2時,返回值= 0

當s1>s2時,返回正數

即:兩個

字串自左向右逐個字元相比(按ascii值大小相比較),直到出現不同的字元或遇'\0'為止。如:

"a"<"b" "a">"a" "computer">"compare"

特別注意:strcmp(const char *s1,const char * s2)這裡面只能比較字串,不能比較數字等其他形式的引數。

當我們要比較兩個int型的陣列怎麼辦呢?

我們可以借助於memcmp()函式,簡介如下:

int memcmp(const void *buf1, const void *buf2, unsigned int count);

比較記憶體區域buf1和buf2的前count個位元組。

當buf1當buf1=buf2時,返回值=0

當buf1>buf2時,返回值》0

具體實現可以是:

int cmp = memcmp(buf1,buf2,sizeof(buf1)<=sizeof(buf2) ? sizeof(buf1)/sizeof(int) : sizeof(buf2)/sizeof(int));

if( 0 == cmp)

比較兩個int型陣列的大小

一般大家都知道比較字元型陣列大小的函式strcmp 下面是strcmp的介紹 原型 extern int strcmp const char s1,const char s2 所在標頭檔案 string.h 功能 比較字串s1和s2。一般形式 strcmp 字串1,字串2 說明 當s1當s1 s2時...

比較兩個時間大小

datetime.compare t1,t2 比較兩個日期大小,排前面的小,排在後面的大,比如 2011 2 1就小於2012 3 2 返回值小於零 t1 小於 t2。返回值等於零 t1 等於 t2。返回值大於零 t1 大於 t2。如 c sharp view plain copy using sy...

比較兩個IP位址大小

define crt secure no warnings 避免不安全錯誤 include include include include include int main int argc,char argv data函式 比較程式 define crt secure no warnings in...