Cpp_Mstring_迭代器

迭代器

可以理解为,指向容器内部数据的一个指针。本质是对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<vector>
int main()
{
vector<int> v1;
for(int i = 0;i<10;i++)
{
v1.push_back(i);
}
vector<int>::iterator it1 = v1.begin();
for(;it1!=v1.end();it1++)
{
cout <<
}
}

Mstring_iterator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//mstring_iterator.h
//begin end insert erase
#ifndef MSTRING_ITERATOR_H
#define MSTRING_ITERATOR_H
class Mstring;

class Mstring_iterator
{
public:
Mstring_iterator(Mstring& mstr, int pos)
:_mstr(mstr), _pos(_pos)
{

}
Mstring_iterator(const Mstring_iterator& src)
:_mstr(src._mstr), _pos(src._pos)
{

}
Mstring_iterator& operator=(const Mstring_iterator& src)
{
if(&_mstr != &src._mstr)
{
return *this;
}
_pos = src._pos;
return *this;
}

bool operator !=(const Mstring_iterator& src)
{
if(&_mstr != &src._mstr || _pos != src._pos)//不是一个字符串或没有指向同一位置
{
return true;
}
return false;
}
bool operator ==(const Mstring_iterator& src)
{
if(&_mstr == &src._mstr && _pos == src._pos)//不是一个字符串或没有指向同一位置
{
return true;
}
return false;
}
Mstring_iterator& operator++()
{
_pos++;
return *this;
}
Mstring_iterator operator++(int)
{
int pos = _pos;
_pos++;
return Mstring_iterator(_mstr,pos);
}

Mstring_iterator& operator--()
{
_pos--;
return *this;
}
Mstring_iterator operator--(int)
{
int pos = _pos;
_pos--;
return Mstring_iterator(_mstr,pos);
}

char& operator*();//由于[]重载在本文件不可见,则在这里实现不可编译,我们可以在mstring.cpp进行类外实现。
private:
Mstring& _mstr;
int _pos;
}

Mstring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//mstring.h
#ifndef MSTRING_H
#define MSTRING_H
#include<iostream>
#include"mstring_iterator.h"
#include<mutex>
using namespace std;

#define MSTRING_MORE_SIZE sizeof(int)
#define DEFEALT_LEN (10+MSTRING_MORE_SIZE)

class Mstring
{
public:
typedef Mstring_iterator iterator;
Mstring(const char* str = NULL);
Mstring(const Mstring& src);
Mstring& operator=(const Mstring& src);
~Mstring();

void push_back(char c);
void pop_back();
char back()const;
char front()const;
bool empty()const;
int size()const;

Mstring operator+(const Mstring& str)const;
char& operator[](int pos);
char operator[](int pos)const;

iterator begin();
iterator end();
void insert(iterator it, char val);
void erase(iterator it);

friend ostream& operator<<(ostream& out, const Mstring& src);
friend istream& operator>>(istream& in, Mstring& src);
private:
bool full()const;
void expand();
void init_num();
int& get_num();
int get_num()const;
const char* get_str_begin()const;
char* get_str_begin();
void write_copy();
int down_num();//引用计数-1
int up_num();//引用计数+1

char* _str;//能否直接使用浅拷贝------怎么加引用计数
int _len;//当前空间总长度
int _val_len;//已经占用的长度,实际数据数量
//static mutex* _lock;
};

#endif

mstring.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include"mstring.h"

mutex* Mstring::_lock = new mutex();

Mstring::Mstring(const char* str)
{
if (NULL == str)
{
_len = DEFEALT_LEN;
_val_len = 0;
_str = new char[_len];
memset(_str, 0, _len);
init_num();
return;
}

_val_len = strlen(str);

//加上引用计数占的空间
_len = _val_len + 1 + MSTRING_MORE_SIZE;
_str = new char[_len];
memset(_str, 0, _len);

for (int i = 0; i < _val_len; i++)
{
get_str_begin()[i] = str[i];
}
init_num();
}

Mstring::Mstring(const Mstring& src)
{
_val_len = src._val_len;
_len = src._len;
_str = src._str;
//让引用计数+1
up_num();
}

Mstring& Mstring::operator=(const Mstring& src)
{
if (&src == this)
{
return *this;
}
_val_len = src._val_len;
_len = src._len;
_str = src._str;
up_num();

return *this;
}

Mstring::~Mstring()
{
//将引用计数-1
down_num();

//查看当前是否还有人引用
if (0 == get_num())
{
delete[]_str;
}
}

void Mstring::push_back(char c)
{
//判断是否一个人独有
if (get_num() > 1)
{
//写时拷贝,给分配独有的空间
write_copy();
}

if (full())
{
expand();
}

get_str_begin()[_val_len] = c;
_val_len++;
}

void Mstring::pop_back()
{
if (get_num() > 1)
{
write_copy();
}

if (empty())
{
return;
}

_val_len--;
}

char Mstring::back()const
{
if (empty())
{
return 0;
}

return get_str_begin()[_val_len - 1];
}

char Mstring::front()const
{
if (empty())
{
return 0;
}

return get_str_begin()[0];
}

bool Mstring::empty()const
{
return _val_len == 0;
}

Mstring Mstring::operator+(const Mstring& str)const
{
char* p;
int len = _val_len + str._val_len + 1;
p = new char[len];
memset(p, 0, len);

//进行数据拷贝-----将两个字符串的数据拼接起来
int i = 0;
for (; i < _val_len; i++)
{
p[i] = get_str_begin()[i];
}

for (int j = 0; j < str._val_len; j++, i++)
{
p[i] = str.get_str_begin()[j];
}

return p;
}

char& Mstring::operator[](int pos)
{
if (get_num() > 1)
{
write_copy();
}
return get_str_begin()[pos];
}

char Mstring::operator[](int pos)const
{
return get_str_begin()[pos];
}

bool Mstring::full()const
{
return _val_len == _len - 1 - MSTRING_MORE_SIZE;
}

void Mstring::expand()
{
if (get_num() > 1)
{
write_copy();
}

_len = _len << 1;

char* p = new char[_len];
memset(p, 0, _len);

for(int i = 0;i<_val_len+MSTRING_MORE_SIZE;i++)
{
p[i] = _str[i];
}

if (down_num() == 0)
{
delete[]_str;
}

_str = p;
}

int Mstring::size()const
{
return _val_len;
}

//初始化引用计数
void Mstring::init_num()
{
get_num() = 1;
}

//获取引用计数
int& Mstring::get_num()
{
return *((int*)_str);
}

//引用计数-1
int Mstring::down_num()
{
_lock->lock();
int num = --get_num();
_lock->unlock();
return num;
}

//引用计数+1
int Mstring::up_num()
{
//get_lock()->lock();
_lock->lock();
/*
while(1)
{
if(_lock->a == 0)
{
_lock->a = 1;
break;
}
sleep(10);
}

*/
int num = ++get_num();
_lock->unlock();
/*
_lock->a = 0;
*/

return num;
}


//获取引用计数
int Mstring::get_num()const
{
return *((int*)_str);
}

//获取字符串的开头指针
char* Mstring::get_str_begin()
{
return _str + MSTRING_MORE_SIZE;
}

//获取字符串的开头指针
const char* Mstring::get_str_begin()const
{
return _str + MSTRING_MORE_SIZE;
}

//写时拷贝
void Mstring::write_copy()
{
char* p = new char[_len];

//将所有的数据全部拷贝过来
for (int i = 0; i < _len; i++)
{
p[i] = _str[i];
}

//改变原来指向的内存的引用计数
if (0 == down_num())
{
delete[]_str;
}

//将当前引用计数改为1
_str = p;
init_num();
}

ostream& operator<<(ostream& out, const Mstring& src)
{
for (int i = 0; i < src.size(); i++)
{
out << src.get_str_begin()[i];
}
out << " :::::num:::" << src.get_num();
out << endl;
return out;
}

istream& operator>>(istream& in, Mstring& src)
{
if (src.get_num() > 1)
{
src.write_copy();
}

char tmp[1024];
in >> tmp;

src = tmp;
return in;
}

////////////////////////////////迭代器

iterator Mstring::begin()
{
return iterator(*this, 0);
}

iterator Mstring::end()
{
return iterator(*this, _val_len);
}
iterator Mstring::insert(iterator it, char val)
{
//判断是否独有
if (get_num() > 1)
{
write_copy();
}

//扩容
if (full())
{
expand();
}

iterator it1 = end();
iterator it2 = it1 - 1;

for (; it1 != it; it1--, it2--)
{
*it1 = *it2;
}
*it = val;
_val_len++;

return it;
}
iterator Mstring::erase(iterator it)
{
//判断是否独有
if (get_num() > 1)
{
write_copy();
}

//判空
if (empty())
{
return it;
}

iterator it1 = it;
iterator it2 = it + 1;
for (; it2 != end(); it1++, it2++)
{
*it1 = *it2;
}

_val_len--;
return it;
}


char& Mstring_iterator::operator*()
{
return _mstr[_pos];
}

test

1
2
3
4
5
6
7
8
9
10
11
int main()
{
Mstring s1 = "123456";
Mstring::iterator it = s1.begin();
for(; it != s1.end(); it++)
{
cout << *it << " ";

}
cout << endl;
}

vector

通过迭代器进行增删改查。

分类

  1. 顺序迭代器
    1. iterator是正向迭代器
    2. reverse_iterator是反向迭代器
    3. const_iterator是常量正向迭代器
    4. const_reverse_iterator是常量反向迭代器
  2. 插入型迭代器
    1. insert_iterator是随机插入迭代器,内部调用的是insert
    2. back_insert_iterator是尾插迭代器,调用的是push_back
    3. front_insert_iterator是头插迭代器,调用的是push_front
  3. 流迭代器

顺序迭代器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//正向迭代打印
template<class T>
void print(T & obj)
{
typename T::iterator it = obj.begin();
for (; it != obj.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
//反向迭代打印
template<class T>
void r_print(T& obj)
{
typename T::reverse_iterator it = obj.rbegin();
for (;it != obj.rend();it++)
{
cout << *it << " ";
}
cout << endl;
}

Cpp_new_delete重载

内容

new的动作

new是关键字。其实是new operator,本身不可重载。

  1. 申请内存
    1. new第一步的申请内存调用的是operator new(可看作一个运算符函数),可以重载。
  2. 调用构造
  3. 返回地址

delete的动作

delete是关键字。其实是delete operator,本身不可重载。

  1. 调用析构
  2. 内存释放
    1. delete内存释放调用的是operator delete(可看作一个运算符函数),可以重载。

operator new重载

1
2
3
4
5
6
void* operator new(size_t size)
{
void* p = malloc(size);
cout << "调用了自定义operator new" << endl;
return p;
}

有何作用?

堆上内存:属于是外部资源。

申请/释放外部资源,用户态没有权限访问,内核态才有权限。外部资源很多,比如我们的鼠标、键盘、屏幕。

我们申请/释放资源的过程需要内核态完成,此时,需要从用户态切换到内核态,再做系统调用

平时接触的系统调用有很多,比如:cout、printf、cin、scanf

应运而生,会出现内存池的概念。C++自带的内存池:_Alloc

内存池是:用户态自己维护的一大段内存。

重载new和delete相当于留下了一个可修改的端口,可以自定义申请、释放内存的动作。这就是重载new和delete的意义所在。
比如,可以自己编写malloc把内存分配到显存中。除了在改变分配内存的不同设备,还可以在这里面进行内存分配动作的统计、标记等,常用于游戏开发、内存池。

但是要知道,C++只允许把分配内存的任务交给程序员自定义,没有开放在重载new中调用构造函数的权限。

operator delete重载

1
2
3
4
5
void operator delete(void* p)
{
cout << "调用了自定义operator delete" << endl;
free(p);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespace std;
class Tmp
{
public:
Tmp()
{
cout << "Tmp()" << endl;
}
~Tmp()
{
cout << "~Tmp()" << endl;
}
void* operator new(size_t size)
{
void* p = malloc(size);
cout << "调用了operator new" << endl;
return p;
}
void operator delete(void* p)
{
cout << "调用了operator delete" << endl;
free(p);
}
};
int main()
{
Tmp* p = new Tmp();

}

作业

  1. mstring三种:普通的、写时拷贝的、迭代器的
  2. 刷题

区别

  1. new是关键字。malloc是函数;
1
2
int * p = new int(10);
int * s = (int*)malloc(sizeof(int));
  1. new不需要类型转换,malloc需要强转
  2. new自动计算类型大小,malloc需要sizeof
  3. new先malloc,再构造,再返回指针。malloc只申请内存
  4. 申请空间失败时
    1. new抛出std::bad_malloc
    2. malloc返回NULL
  5. new可以申请一组,需要new[],delete[]。而malloc必须手动指明字节数,free不知道释放的是不是一组。
  6. 对于内置类型、POD类型,new/malloc可以混用;对于自定义类型能否混用?主要看类型中有没有析构函数,如果有,在new[]时会记录申请了多少对象,于是在delete[]时可以自动溯源。
  7. new是运算符,可以进行重载;malloc则不行
  8. new有三种调用方式;malloc没有
  9. new可以设置nothrow不抛异常,返回NULL
  10. set_new_handler