-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCArray.h
More file actions
75 lines (75 loc) · 1.38 KB
/
Copy pathCArray.h
File metadata and controls
75 lines (75 loc) · 1.38 KB
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
#pragma once
#include <iostream>
//可变大小的数组
class CArray {
int Capacity;
int Size;
int *ptr;
public:
CArray(int s = 0);
CArray(CArray &a);//拷贝构造函数
~CArray();
void push_back(int V);
CArray &operator=(const CArray &a);//需要深拷贝
int size() { return Size; }
int capacity() { return Capacity; }
int &operator[](int i) {
return ptr[i];
}
};
CArray::CArray(int s) :Capacity(s),Size(0){
if (s == 0)
ptr = NULL;
else
ptr = new int[s];
}
CArray::CArray(CArray &a) {//复制构造函数
if (!a.ptr) {//a.ptr为空指针
ptr = NULL;
Size = 0;
Capacity = 0;
return;
}
ptr = new int[a.Capacity];
memcpy(ptr, a.ptr, sizeof(int)*a.Size);
Size = a.Size;
Capacity = a.Capacity;
}
CArray::~CArray() {
if (ptr) delete[]ptr;//ptr不为空,释放空间
}
CArray &CArray::operator=(const CArray &a){
if (ptr == a.ptr)
return *this;
if (a.ptr == NULL) {
if (ptr) delete[]ptr;
ptr = NULL;
Size = 0;
Capacity = 0;
return *this;
}
if (Capacity!=a.Capacity) {
if (ptr) delete[]ptr;
ptr = new int[a.Capacity];
}
memcpy(ptr, a.ptr, sizeof(int)*a.Size);
Size = a.Size;
Capacity = a.Capacity;
return *this;
}
void CArray::push_back(int v) {
if(ptr==NULL){
ptr = new int[32];
Capacity += 32;
}
else{
if (Size == Capacity) {
int *temptr = new int[Capacity * 2];
memcpy(temptr, ptr, sizeof(int)*Size);
delete[]ptr;
ptr = temptr;
Capacity *= 2;
}
}
ptr[Size++] = v;
}