• <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>

            阿攀的博客

            海闊天空

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              5 隨筆 :: 2 文章 :: 11 評論 :: 0 Trackbacks

                  代碼下載鏈接: /Files/Apan/CP_FixedString.rar
                 
                   近來,項目組中在很多地方頻繁的使用固定長度的字符數組,由于各人的操作習慣不一樣,可能要的結果一樣,但過程不一致,有時,在書寫過程中,可能會漏寫致命的操作。基于這些原因,封裝了一個固定長度字符數組的模板類容器,提供一些常用操作,如果需要更多的操作,可以使用STL里的算法。代碼如下:
                   注意:由于,模板的參數是個常量,所以CP_String<10>test1和CP_String<16>test2是兩種不同類型的對象,這樣的操作CP_String<10>test1(test2)、test1=test2等是非法的,可以這樣使用CP_String<10>test1(test2.begin())、test1=test2.begin()
                   希望對看過本篇文章的朋友有所幫助,如發現錯誤的地方,請指出,非常感謝!
                  
                  后記:寫完本片文章的第二天,感覺這樣的操作CP_String<10>test1(test2)、test1=test2等是非法的,感覺不是很爽,用起來比較麻煩,經過一番的嘗試,終于搞定,很是高興。
                              拷貝構造函數、賦值函數等類似函數只需這樣修改就可以:

            1 template<size_type M>
            2     CP_String(const CP_String<M>& other)
            3     {
            4         stringCopy(other.begin());
            5     }

             這是原來寫法:

            1 
            2     CP_String(const CP_String<N>& other)
            3     {
            4         stringCopy(other.begin());
            5     }


            完整代碼如下:

              1 #ifndef _HZ_FIXSTRING_HEAD
              2 #define _HZ_FIXSTRING_HEAD
              3 /**
              4  CopyRight:  (C)  2009 by caipan
              5  Contents:   Fixed buffer length strings
              6  Email:      caipan0206@gmail.com
              7 */
              8 
              9 #include <memory.h>
             10 
             11 template<size_t N>
             12 class CP_FixString
             13 {
             14 public:
             15     typedef TCHAR        value_type;
             16     typedef TCHAR*       pointer;
             17     typedef const TCHAR* const_pointer;
             18     typedef TCHAR*       iterator;
             19     typedef const TCHAR* const_iterator;
             20     typedef TCHAR&       reference;
             21     typedef const TCHAR& const_reference;
             22     typedef size_t       size_type;
             23     typedef ptrdiff_t    difference_type;
             24     typedef std::random_access_iterator_tag iterator_category;
             25 
             26 private:
             27     enum 
             28     {
             29         max_length = N, //表示字符串的最大長度,不包括_T('\0')
             30         max_size = N+1  //容納字符串的緩沖區大小
             31     }; 
             32 
             33 public:
             34     CP_FixString()
             35     {
             36         m_nStrLength = 0;
             37         memset(m_buf, 0sizeof(m_buf));
             38     }
             39 
             40     CP_FixString(const TCHAR *pData)
             41     {
             42         stringCopy(pData);
             43     }
             44 
             45     template<size_type M>
             46     CP_FixString(const CP_FixString<M>& other)
             47     {
             48         stringCopy(other.begin());
             49     }
             50 
             51 public:
             52     template<size_type M>
             53     CP_FixString& operator=(const CP_FixString<M> &other)
             54     {
             55         stringCopy(other.begin());
             56         return *this;
             57     }
             58 
             59     CP_FixString& operator=(const TCHAR *pData)
             60     {
             61         stringCopy(pData);
             62         return *this;
             63     }
             64 
             65     CP_FixString& operator+=(const TCHAR *pData)
             66     {
             67         stringCat(pData);
             68         return *this;
             69     }
             70 
             71     template<size_type M>
             72     CP_FixString& operator+=(const CP_FixString<M>& other)
             73     {
             74         stringCat(other.begin());
             75         return *this;
             76     }
             77 
             78     bool operator==(const TCHAR *pData)
             79     {
             80         return !_tcscmp(m_buf, pData);
             81     }
             82 
             83     template<size_type M>
             84     bool operator==(const CP_FixString<M>& other)
             85     {
             86         return !_tcscmp(m_buf, other.begin());
             87     }
             88 
             89     reference operator[](size_type n)
             90     {
             91         n = n < max_length ? n : max_length;
             92 
             93         return m_buf[n];
             94     }
             95 
             96     const_reference operator[] (size_type n) const
             97     {
             98         n = n < max_length ? n : max_length;
             99 
            100         return m_buf[n];
            101     }
            102 
            103 public:
            104     iterator begin()
            105     {
            106         return m_buf;
            107     }
            108 
            109     iterator end()
            110     {
            111         return m_buf + m_nStrLength + 1;
            112     }
            113 
            114     const_iterator begin() const
            115     {
            116         return m_buf;
            117     }
            118 
            119     const_iterator end() const
            120     {
            121         return m_buf + m_nStrLength + 1;
            122     }
            123 
            124     bool empty()
            125     {
            126         return m_buf[0== _T('\0');
            127     }
            128 
            129     void clear()
            130     {
            131         m_nStrLength = 0;
            132         memset(m_buf, 0sizeof(m_buf));
            133     }
            134 
            135     size_type capacity() const
            136     {
            137         return max_length;
            138     }
            139 
            140     size_type size() const
            141     {
            142         return m_nStrLength;
            143     }
            144 
            145     size_type length() const
            146     {
            147         return m_nStrLength;
            148     }
            149 
            150 private:
            151     void stringCopy(const TCHAR *pData)
            152     {
            153         memset(m_buf, 0sizeof(m_buf)); 
            154 
            155         size_type nLen = _tcslen(pData);
            156 
            157         nLen = nLen < max_length ? nLen : max_length;
            158 
            159         _tcsncpy(m_buf, pData, nLen);
            160    
            161         m_nStrLength = _tcslen(m_buf);
            162     }
            163 
            164     void stringCat(const TCHAR *pData)
            165     {
            166         size_type nDataLen = _tcslen(pData);
            167         size_type nLen = length();
            168         nLen = max_length - nLen;
            169         nLen = nLen > nDataLen ? nDataLen : nLen;
            170 
            171         _tcsncat(m_buf, pData, nLen);
            172         
            173         m_nStrLength = _tcslen(m_buf);
            174     }
            175 
            176 private:
            177     TCHAR m_buf[max_size];
            178     size_type m_nStrLength;
            179 };
            180 #endif
            posted on 2009-05-02 18:48 阿攀 閱讀(2119) 評論(4)  編輯 收藏 引用 所屬分類: STL

            評論

            # re: 封裝固定長度字符數組的模板容器類 2009-05-02 22:58 尹東斐
            我覺得這個可以考慮重新定義一個
            template <class T, int N>
            class my_allocator
            : public allocator<T>
            {
            //按照N分配空間
            };

            template <int N>
            class my_string
            : public basic_string<char, char_traits<char>, my_allocator<char, N> >
            {};

            這樣子實現起來,不用考慮異常安全等問題,標準庫會考慮這個,因為allocator的實現比起string來,簡單多了。  回復  更多評論
              

            # re: 封裝固定長度字符數組的模板容器類 2009-05-04 08:51 Apan
            看來這位仁兄是這方面的老手。謝謝你的建議!@尹東斐
              回復  更多評論
              

            # re: 封裝固定長度字符數組的模板容器類 2010-02-23 18:48 ccsdu2009
            boost中就有array!  回復  更多評論
              

            # re: 封裝固定長度字符數組的模板容器類[未登錄] 2010-02-24 15:30 Apan
            boost中確實有這樣的fixed array template,但個人感覺并不適合于fixed string。@ccsdu2009
              回復  更多評論
              

            国产一区二区精品久久| 无码人妻少妇久久中文字幕| 7国产欧美日韩综合天堂中文久久久久 | 久久久WWW成人| 亚洲午夜久久久久妓女影院| 99蜜桃臀久久久欧美精品网站 | 久久精品免费一区二区三区| 1000部精品久久久久久久久| 一本久久精品一区二区| 一本色道久久88加勒比—综合| 香蕉久久夜色精品国产2020| 久久97精品久久久久久久不卡| 久久精品国产亚洲AV香蕉| 999久久久免费国产精品播放| 亚洲va久久久噜噜噜久久狠狠| 久久午夜无码鲁丝片午夜精品| 91精品国产高清久久久久久io| 伊人色综合久久天天人手人婷| 久久久精品国产亚洲成人满18免费网站 | 99久久综合狠狠综合久久| 久久久久国产精品熟女影院| 伊人久久一区二区三区无码| 国产成人精品久久亚洲高清不卡| 精品国产乱码久久久久久1区2区| 久久婷婷五月综合成人D啪| 天天影视色香欲综合久久| 狠狠色丁香婷婷综合久久来来去| 精品乱码久久久久久久| 无码专区久久综合久中文字幕| 久久久久久久精品妇女99| 武侠古典久久婷婷狼人伊人| 久久一本综合| 人人狠狠综合久久亚洲| 久久久久国产一级毛片高清板| 国产精品欧美久久久久无广告| 久久久国产精品福利免费 | 狠狠色综合网站久久久久久久高清| 久久久久久国产精品美女| 久久AⅤ人妻少妇嫩草影院| 久久国产乱子伦精品免费午夜| 久久久噜噜噜久久中文字幕色伊伊|