-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharerase.h
77 lines (61 loc) · 1.17 KB
/
arerase.h
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
#ifndef ARERASE_H
#define ARERASE_H
#include <algorithm>
template <class T>
class array_eraser
{
private:
T *_array;
int _capacity;
int _size;
public:
array_eraser(T *arr, int size) : _array(arr), _capacity(size), _size(size) {}
int size() const
{
return _size;
}
void erase(const T &elem)
{
for (int i = _size - 1; i >= 0; i--)
{
if (_array[i] == elem)
{
erase_index(i);
}
}
}
void erase_index(int index)
{
if (index >= _size)
return;
T temp = _array[index];
for (int i = index; i < _size - 1; i++)
{
_array[i] = _array[i + 1];
}
_array[_size - 1] = temp;
_size--;
}
int count(const T& elem) const
{
int count = 0;
for(int i = 0; i < _size; i++)
{
if(_array[i] == elem)
{
count++;
}
}
return count;
}
typedef T* iterator;
iterator begin()
{
return _array;
}
iterator end()
{
return _array + _size;
}
};
#endif