• <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
            函數:int memcmp (const void *a1, const void *a2, size_t size) 
                   函數memcmp用于比較字符串s1與s2的前size個字符。
                  如果兩上字符塊相同,memcmp將返回0。

            函數:int strcmp (const char *s1, const char *s2) 
                    這個函數用來比較s1和s2字符串,這個函數將返回一個值,它的符號與第一對不同的字符的比較結果相關。
                   如果兩個字符串相等的話,strcmp將返回0。
                   如果s1是s2的一個子串的話,s1小于s2
            此外還有函數 
                int strncmp (const char *s1, const char *s2, size_t size) 
                此函數與strcmp極為類似。不同之處是,strncmp函數是指定比較size個字符。也就是說,如果字符串s1與s2的前size個字符相同,函數返回值為0。

            功能比較:

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

            效率差異:
                 strcmp比較的字符串,而memcmp比較的是內存塊,strcmp需要時刻檢查是否遇到了字符串結束的 \0 字符,而memcmp則完全不用擔心這個問題,所以memcmp的效率要高于strcmp

            使用示例:

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

             附: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++)                  //比較到結束符\0,時,已經做了__res = *cs - *ct了,所以不等長度時,肯定返回不為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;
            }

            轉自:

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


            posted on 2009-11-24 12:04 chatler 閱讀(1873) 評論(0)  編輯 收藏 引用 所屬分類: C++_BASIS
            <2010年1月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(10)

            隨筆分類(307)

            隨筆檔案(297)

            algorithm

            Books_Free_Online

            C++

            database

            Linux

            Linux shell

            linux socket

            misce

            • cloudward
            • 感覺這個博客還是不錯,雖然做的東西和我不大相關,覺得看看還是有好處的

            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

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            成人久久精品一区二区三区| 精品多毛少妇人妻AV免费久久| 一级a性色生活片久久无| 中文字幕无码免费久久| 久久美女人爽女人爽| 久久免费看黄a级毛片| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久婷婷五月综合97色一本一本| 久久夜色撩人精品国产| 国产亚洲精久久久久久无码77777 国产亚洲精品久久久久秋霞 | 久久精品国产黑森林| 久久久久AV综合网成人 | 久久青青草原精品国产| 欧美亚洲国产精品久久高清| 亚洲一区二区三区日本久久九| 国产精品九九九久久九九 | 久久精品免费观看| 一级做a爰片久久毛片16| 99久久国产综合精品五月天喷水 | 久久中文字幕无码专区 | 国产精品99久久不卡| 人妻少妇精品久久| 久久毛片一区二区| 久久九九兔免费精品6| 久久夜色精品国产噜噜亚洲AV| 久久成人国产精品免费软件| 久久久久久精品成人免费图片| 中文成人久久久久影院免费观看| 久久强奷乱码老熟女| 狠狠色婷婷久久一区二区| 久久免费高清视频| 亚洲AV日韩精品久久久久| 久久无码av三级| 无码日韩人妻精品久久蜜桃| 国产亚洲美女精品久久久久狼| 国内精品久久久久久中文字幕| 久久人人爽人人爽人人片AV麻烦| 91久久精品视频| 1000部精品久久久久久久久| 久久精品免费一区二区| 99久久精品国产毛片|