吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1688|回复: 3
收起左侧

[C&C++ 原创] C++迭代器模拟代码(学习记录)

[复制链接]
天上飞来一只 发表于 2020-10-23 10:33
本帖最后由 天上飞来一只 于 2020-10-23 11:37 编辑

学习使用,没大规模测试,通用改模板
留点分,谢谢!

[C++] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <memory.h>#include <stdexcept>
#include <assert.h>
 
class CArrary
{
public:
        //迭代器类,保护成员
        class iterator
        {
        public:
                iterator& operator++()
                {
                        assert(m_data != m_end);
                        m_data++;
                        return *this;
                }
                iterator& operator--()
                {
                        assert(m_data != m_begin);
                        m_data--;
                        return *this;
                }
                iterator operator++(int)
                {
                        assert(m_data != m_end);
                        //------注意,临时变量保存,在构造
                        char* temp = m_data;
                        m_data++;
                        return iterator(temp, m_begin, m_end);
                }
                iterator operator--(int)
                {
                        assert(m_data != m_begin);
                        //------注意,临时变量保存,在构造
                        char* temp = m_data;
                        m_data--;
                        return iterator(temp, m_begin, m_end);
                }
                char& operator*()
                {
                        return *m_data;
                }
                bool operator==(const iterator& itr)
                {
                        return m_data == itr.m_data;
                }
                bool operator!=(const iterator& itr)
                {
                        return m_data != itr.m_data;
                }
 
        private:
                friend class CArrary;
                //私有构造器,只能通过公共方法获取
                iterator(char *data, char *begin, char *end)
                {
                        m_data = data;
                        m_begin = begin;
                        m_end = end;
                }
        private:
                char* m_data;//保护的数据
                char* m_begin;//开始数据
                char* m_end;//结束
        };
        //类外iterator成员方法
public:
        //必须有 begin end
        //----------注意,头与尾,必须越界,这样++判断是否越界
        iterator begin()
        {
                return iterator(m_pBuff, m_pBuff - 1, m_pBuff + m_nElementSize);
        }
        iterator end()
        {
                return iterator(m_pBuff + m_nElementSize, m_pBuff - 1, m_pBuff + m_nElementSize);
        }
public:
        CArrary();
        //直接深拷贝
        CArrary(const CArrary& ary);
        virtual ~CArrary();
 
public:
        //初始化
        void InitFun();
        //增加
        bool AddHead(int val);
        bool AddTail(int val);
        bool Insert(int nIdx, int val);
        //--------------重载迭代器,待完成
        bool Insert(iterator itr, int val)
        {
                //利用数组的连续性,求长度
                /*
                int arr[10] = { 0 };
                int i = &arr[5] - arr;
                cout << i<< endl;
                */
                int nIndex = itr.m_data - m_pBuff;
                Insert(nIndex, val);
        }
        //删除
        bool RemoveHead();
        bool RemoveTail();
        bool Remove(int nIdx);
        //--------------重载迭代器,待完成
        bool Remove(iterator itr)
        {
                int nIndex = itr.m_data - m_pBuff;
                Remove(nIndex);
        }
 
        //修改
        bool SetVal(int nIdx, int val);
        //--------------重载迭代器,待完成
        bool SetVal(iterator itr, int val) {}
        char&  operator[](int nIdx);//此处会抛出异常
        CArrary& operator+=(const CArrary& ary);
        CArrary& operator=(const CArrary& ary);//直接深拷贝
 
                                                                                   //查询
        int Find(int val);
 
        //获取数组元素的个数
        int Size()const;
 
        //清空
        void Clear();
 
private:
        char *m_pBuff;            //存放数组元素的缓冲区
        int   m_nBuffSize;        //缓冲区的大小
        int   m_nElementSize;      //数组中元素的个数
};
 
void CArrary::InitFun()
{
     m_pBuff = nullptr;
     m_nElementSize = 0;
     m_nBuffSize = 0;
}
 
CArrary::CArrary()
{
         
     InitFun();
}
 
 
CArrary::CArrary(const CArrary& ary)
{
        //初始化,防止没有调用构造函数;
        InitFun();
        *this = ary;
}
 
 
CArrary::~CArrary()
{
        Clear();
}
 
 
bool CArrary::AddHead(int val)
{
        return Insert(0, val);
}
 
bool CArrary::AddTail(int val)
{
        return Insert(m_nElementSize, val);
}
 
 
bool CArrary::Insert(int nIdx, int val)
{
        //判断是否为空
        if (m_pBuff == nullptr)
        {
                const int nInitBuffSize = 4;
                m_nBuffSize = nInitBuffSize;
                m_pBuff = new char[m_nBuffSize];
                m_nElementSize = 0;
        }
 
        //判断索引值是否合理
        if (nIdx < 0 || nIdx > m_nElementSize)
        {
                return false;
        }
 
        //判断缓冲区是否足够
        if (m_nElementSize >= m_nBuffSize)
        {
                //申请新的缓冲区
                m_nBuffSize = m_nBuffSize * 2; //原缓冲区扩大两倍
                char* pNewBuff = new char[m_nBuffSize];
                memcpy(pNewBuff, m_pBuff, m_nElementSize * sizeof(char)); //拷贝原来的数据
                delete[]m_pBuff; //删除原来的缓冲区
                m_pBuff = pNewBuff;
        }
 
        //添加新的元素
        //从nIdx开始的元素都向后移动
        memcpy(&m_pBuff[nIdx + 1], &m_pBuff[nIdx], (m_nElementSize - nIdx) * sizeof(char));
        m_pBuff[nIdx] = val;
        m_nElementSize++;
 
        return true;
}
 
 
bool CArrary::RemoveHead()
{
        return Remove(0);
}
 
 
bool CArrary::RemoveTail()
{
        return Remove(m_nElementSize - 1);
}
 
 
bool CArrary::Remove(int nIdx)
{
        //数组为空则不删除  判断索引值是否合理
        if (m_pBuff == nullptr || nIdx < 0 || nIdx >= m_nElementSize)
        {
                return false;
        }
 
        //删除元素
        memcpy(&m_pBuff[nIdx],
                &m_pBuff[nIdx + 1],
                (m_nElementSize - nIdx - 1) * sizeof(char));
        m_nElementSize--;
 
        return true;
}
 
 
bool CArrary::SetVal(int nIdx, int val)
{
        //判断索引值是否合理
        if (nIdx < 0 || nIdx >= m_nElementSize)
        {
                return false;
        }
 
        //修改
        m_pBuff[nIdx] = val;
 
        return true;
}
 
 
char&  CArrary::operator[](int nIdx)
{
        //判断索引值是否合理
        if (nIdx < 0 || nIdx >= m_nElementSize || m_pBuff == nullptr)
        {
                throw std::out_of_range("访问越界");
        }
 
        return m_pBuff[nIdx];
}
 
CArrary& CArrary::operator+=(const CArrary & ary)
{
        //如果对方为空, 则不拼接
        if (ary.m_pBuff == nullptr)
        {
                return *this;
        }
 
        //如果自己为空,则直接拷贝
        if (m_pBuff == nullptr)
        {
                *this = ary;
                return *this;
        }
 
        //申请新的内存, 拷贝数据
 
        char* pNewBuff = new char[m_nBuffSize + ary.m_nBuffSize];
 
        //防止元素是指针、地址的情况
        for (int i = 0; i < m_nElementSize; i++)
        {
                pNewBuff[i] = m_pBuff[i];
        }
        for (int i = 0; i < ary.m_nElementSize; i++)
        {
                pNewBuff[m_nElementSize + i] = ary.m_pBuff[i];
        }
        //memcpy(pNewBuff, m_pBuff, m_nElementSize * sizeof(T));
        //memcpy(pNewBuff + m_nElementSize, ary.m_pBuff, ary.m_nElementSize * sizeof(T));
 
        delete[] m_pBuff;
        m_pBuff = pNewBuff;
        m_nBuffSize += ary.m_nBuffSize;
        m_nElementSize += ary.m_nElementSize;
        return *this;
}
 
 
CArrary& CArrary::operator=(const CArrary & ary)
{
        //自己赋值自己, 不做任何操作
        if (this == &ary)
        {
                return *this;
        }
        //拷贝目标对象的数据到自己
        m_pBuff = new char[ary.m_nBuffSize];
        //防止元素是指针、地址的情况
        for (int i = 0; i < ary.m_nElementSize; i++)
        {
                m_pBuff[i] = ary.m_pBuff[i];
        }
        m_nElementSize = ary.m_nElementSize;
        m_nBuffSize = ary.m_nBuffSize;
 
        return *this;
}
 
 
int CArrary::Find(int val)
{
        for (int i = 0; i < m_nElementSize; i++)
        {
                if (m_pBuff[i] == val)
                {
                        return i;
                }
        }
 
        return -1;
}
 
 
int CArrary::Size() const
{
        return m_nElementSize;
}
 
 
void CArrary::Clear()
{
        if (m_pBuff != nullptr)
        {
                delete[] m_pBuff;
                m_pBuff = nullptr;
                m_nBuffSize = 0;
                m_nElementSize = 0;
        }
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

284406022 发表于 2020-10-23 11:01
CArrary  这个类测试过吗?  稳定不?
sjh52pojie 发表于 2020-10-23 11:21
 楼主| 天上飞来一只 发表于 2020-10-23 11:36
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-10-9 09:32

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表