• <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
              回復  更多評論
              

            99久久免费国产精品特黄| 亚洲乱码精品久久久久..| 色综合合久久天天综合绕视看| 久久久无码精品亚洲日韩蜜臀浪潮| 久久天天躁狠狠躁夜夜网站 | 精品国产热久久久福利| 日韩va亚洲va欧美va久久| 久久狠狠爱亚洲综合影院 | 99久久99久久精品国产片果冻 | 国产精品伊人久久伊人电影 | 国产精品熟女福利久久AV| 三级三级久久三级久久| 久久亚洲欧美日本精品| 中文精品99久久国产| 国内精品久久九九国产精品| 久久久久国产一区二区| 国产日产久久高清欧美一区| 漂亮人妻被中出中文字幕久久| 久久国产精品-国产精品| 久久久久亚洲AV成人网人人网站| 成人国内精品久久久久影院VR| 无码AV波多野结衣久久| 亚洲欧洲中文日韩久久AV乱码| 久久久久久久综合日本亚洲| 久久久久成人精品无码中文字幕| 色综合久久天天综线观看| 亚洲狠狠久久综合一区77777 | 麻豆久久| 久久精品18| 久久久久久久综合日本| 91精品国产91热久久久久福利| 99久久久国产精品免费无卡顿| 午夜精品久久久久久99热| 99久久这里只精品国产免费| 欧美一区二区久久精品| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久国产精品无码网站| 成人亚洲欧美久久久久| 国产精品女同一区二区久久| 国产综合精品久久亚洲| 国产精品热久久毛片|