封装固定长度字符数组的模板容器类

本文介绍了一种用于处理固定长度字符数组的模板类容器CP_FixString,该容器提供了多种实用的操作方法,并解决了不同长度类型间的转换问题。

近来,项目组中在很多地方频繁的使用固定长度的字符数组,由于各人的操作习惯不一样,可能要的结果一样,但过程不一致,有时,在书写过程中,可能会漏写致命的操作。基于这些原因,封装了一个固定长度字符数组的模板类容器,提供一些常用操作,如果需要更多的操作,可以使用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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值