-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegerArray.h
134 lines (112 loc) · 4.05 KB
/
IntegerArray.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
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
//
// Created by 22355 on 24-6-2.
//
#ifndef INTEGERARRAY_H
#define INTEGERARRAY_H
#include <stdexcept>
#include <string>
#include<iostream>
namespace Sika {
class IntegerArray {
int *_array;
int _size;
// 让类外的函数能够访问私有成员
friend std::ostream &operator<<(std::ostream &print, const IntegerArray &ia);
public:
/*-------------------- 构造 --------------------*/
IntegerArray() {
this->_size = 0;
this->_array = new int[0];
}
explicit IntegerArray(const int size) {
this->_size = size;
this->_array = new int[this->_size];
for (int i = 0; i < this->_size; i++) {
this->_array[i] = 0;
}
}
IntegerArray(const int size, const int defaultValue) {
this->_size = size;
this->_array = new int[size];
for (int i = 0; i < this->_size; i++) {
this->_array[i] = defaultValue;
}
}
~IntegerArray() {
delete[] _array;
}
/*------------------- 必须重写拷贝构造函数, 让后置自增的效果正常 -------------------*/
IntegerArray(const IntegerArray& integer_array) {
this->_size = integer_array._size;
this->_array = new int[_size];
for (int i = 0; i < this->_size; i++) {
this->_array[i] = integer_array[i];
}
}
/*------------------- 属性方法 -------------------*/
[[nodiscard]] int size() const {
return this->_size;
}
/*------------------- 运算符重载 -------------------*/
int operator[](const int index) const {
if (index < 0) {
throw std::invalid_argument("索引不能为负数");
}
if (index >= this->_size) {
throw std::out_of_range("index out of range _array");
}
return this->_array[index];
}
void operator+(const int num) const {
for (int i = 0; i < _size; ++i) {
this->_array[i] += num;
}
}
/*------------------- 前置自增运算符重载 -------------------*/
// 如果不返回引用而是返回值, 多次++操作的不是同一个对象 ++(++ia)操作的是两个对象
IntegerArray& operator++(){
for (int i = 0; i < _size; i++) {
_array[i]++;
}
return *this;
}
/*------------------- 后置自增运算符重载 使用int参数占位 -------------------*/
// 后置自增不能返回引用, 在函数结束后对象会被销毁, 如果返回引用后续的操作是对一个被销毁的对象操作导致错误
IntegerArray operator++(int) const {
const IntegerArray temp = *this;
for (int i = 0; i < _size; i++) {
_array[i]++;
}
return temp;
}
IntegerArray& operator--() {
for (int i = 0; i < _size; i++) {
_array[i]--;
}
return *this;
}
IntegerArray operator--(int) const {
const IntegerArray temp = *this;
for (int i = 0; i < _size; i++) {
_array[i]--;
}
return temp;
}
};
/*------------------- ostream输出对象需要类外实现, 并且不能是成员函数 -------------------*/\
inline std::ostream &operator<<(std::ostream &print, const IntegerArray &ia) {
std::string target = "[";
for (int i = 0; i < ia.size(); ++i) {
target.append(std::to_string(ia._array[i]) + ", ");
}
if (const auto lastComma = target.find_last_of(", "); lastComma == target.size() - 1) {
// target.pop_back();
// target.pop_back();
target = target.substr(0, lastComma-1);
}
target.append("]");
print << target;
return print;
}
} // Sika
#endif //INTEGERARRAY_H