• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 297,  comments - 15,  trackbacks - 0
            函數(shù):int memcmp (const void *a1, const void *a2, size_t size) 
                   函數(shù)memcmp用于比較字符串s1與s2的前size個(gè)字符。
                  如果兩上字符塊相同,memcmp將返回0。

            函數(shù):int strcmp (const char *s1, const char *s2) 
                    這個(gè)函數(shù)用來(lái)比較s1和s2字符串,這個(gè)函數(shù)將返回一個(gè)值,它的符號(hào)與第一對(duì)不同的字符的比較結(jié)果相關(guān)。
                   如果兩個(gè)字符串相等的話,strcmp將返回0。
                   如果s1是s2的一個(gè)子串的話,s1小于s2
            此外還有函數(shù) 
                int strncmp (const char *s1, const char *s2, size_t size) 
                此函數(shù)與strcmp極為類似。不同之處是,strncmp函數(shù)是指定比較size個(gè)字符。也就是說(shuō),如果字符串s1與s2的前size個(gè)字符相同,函數(shù)返回值為0。

            功能比較:

                 二者都可以用于字符串的比較,但是二者是有比較大的差異的,因?yàn)閟trcmp是按照字節(jié)(byte-wise)比較的,并且比較的過(guò)程中會(huì)檢查是否出現(xiàn)了"\0"結(jié)束符,一旦任意一個(gè)字符串指針前進(jìn)過(guò)程中遇到結(jié)束符,將終止比較。而memcmp函數(shù)是用于比較兩個(gè)內(nèi)存塊的內(nèi)容是否相等,在用于字符串比較時(shí)通常用于測(cè)試字符串是否相等,不常進(jìn)行byte-wise的字符串比較。如果要比較的對(duì)象中包含一些由于邊界對(duì)齊需求而填入結(jié)構(gòu)對(duì)象中的空格、聯(lián)合 (union)結(jié)束的額外空格、字符串所分配的空間未使用完的部分引起的“holes”的話,最好使用memcmp來(lái)完成。這些“holes”的內(nèi)容是不確定的,在執(zhí)行byte-wise比較時(shí)結(jié)果也是不明確的。

            效率差異:
                 strcmp比較的字符串,而memcmp比較的是內(nèi)存塊,strcmp需要時(shí)刻檢查是否遇到了字符串結(jié)束的 \0 字符,而memcmp則完全不用擔(dān)心這個(gè)問(wèn)題,所以memcmp的效率要高于strcmp

            使用示例:

            給出一個(gè)如下的結(jié)構(gòu)定義:
            struct foo
              {
                unsigned char tag;
                union
                   {
                     double f;
                    long i;
                      char *p;
                   } value;
              };
                  如果要比較兩個(gè)struct foo對(duì)象的話,建議最好使用memcmp。
                 在給出一個(gè)字符串比較的例子,判斷字符串str中前四個(gè)中字符是否為 0x80100001,因?yàn)?x00對(duì)于字符串而言,這是個(gè)結(jié)束符,如果使用strncmp的話strncmp(str,"\x80\x10\x00 \x01",4)的話,實(shí)際效果是只判斷了是否含有0x8010,也就是說(shuō)一旦str中前兩個(gè)字符為0x8010就返回0,表示相同了,顯然這是不正確的!此時(shí)應(yīng)該使用memcmp(str,"\x80\x10\x00\x01",4),這樣一來(lái)就達(dá)到了目的

             附:strcmp,strncmp,memcmp的Linux的源代碼

            /**
             * strcmp - Compare two strings
             * @cs: One string
             * @ct: Another string
             */
            int strcmp(const char *cs, const char *ct)
            {
                    signed char __res;

                    while (1) {
                            if ((__res = *cs - *ct++) != 0 || !*cs++)
                                    break;
                    }
                    return __res;
            }

             /**
             * strncmp - Compare two length-limited strings
             * @cs: One string
             * @ct: Another string
             * @count: The maximum number of bytes to compare
             */
            int strncmp(const char *cs, const char *ct, size_t count)
            {
                    signed char __res = 0;

                    while (count) {
                            if ((__res = *cs - *ct++) != 0 || !*cs++)                  //比較到結(jié)束符\0,時(shí),已經(jīng)做了__res = *cs - *ct了,所以不等長(zhǎng)度時(shí),肯定返回不為0
                                    break;
                            count--;
                    }
                    return __res;
            }
            /**
             * memcmp - Compare two areas of memory
             * @cs: One area of memory
             * @ct: Another area of memory
             * @count: The size of the area.
             */
            int memcmp(const void *cs, const void *ct, size_t count)
            {
                    const unsigned char *su1, *su2;
                    int res = 0;
                    for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
                            if ((res = *su1 - *su2) != 0)
                                    break;
                    return res;
            }

            轉(zhuǎn)自:

            http://blog.chinaunix.net/u2/67780/showart_2079171.html


            posted on 2009-11-24 12:04 chatler 閱讀(1878) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++_BASIS
            <2009年11月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(10)

            隨筆分類(307)

            隨筆檔案(297)

            algorithm

            Books_Free_Online

            C++

            database

            Linux

            Linux shell

            linux socket

            misce

            • cloudward
            • 感覺(jué)這個(gè)博客還是不錯(cuò),雖然做的東西和我不大相關(guān),覺(jué)得看看還是有好處的

            network

            OSS

            • Google Android
            • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
            • os161 file list

            overall

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            波多野结衣久久一区二区| 99久久99久久精品免费看蜜桃| 国产毛片久久久久久国产毛片 | 久久久精品2019免费观看| 久久久久久人妻无码| 18禁黄久久久AAA片| 久久天天躁夜夜躁狠狠| 国产精品久久永久免费| 精品久久久久久久国产潘金莲| 久久久久亚洲AV无码麻豆| 久久精品三级视频| 精品国产乱码久久久久久郑州公司| 精品久久久久久亚洲精品 | 国产三级久久久精品麻豆三级 | 亚洲va中文字幕无码久久| 99久久99久久精品国产| 久久精品国产亚洲av麻豆色欲 | 久久国产欧美日韩精品| 一本久久a久久精品综合香蕉| 97久久国产亚洲精品超碰热| 久久久国产99久久国产一| 成人a毛片久久免费播放| 久久久亚洲欧洲日产国码aⅴ| 中文成人无码精品久久久不卡| 51久久夜色精品国产| 精品免费tv久久久久久久| 亚洲国产精品18久久久久久| 伊人久久成人成综合网222| 99久久www免费人成精品 | 免费无码国产欧美久久18| 久久性精品| 久久久精品国产Sm最大网站| 国产成人精品综合久久久| 中文字幕一区二区三区久久网站| 狠狠88综合久久久久综合网| jizzjizz国产精品久久| 久久婷婷五月综合国产尤物app| 亚洲αv久久久噜噜噜噜噜| 无码日韩人妻精品久久蜜桃| 久久综合噜噜激激的五月天| 久久久久无码精品国产|