-
Notifications
You must be signed in to change notification settings - Fork 21
/
array.cpp
98 lines (77 loc) · 1.82 KB
/
array.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
//
// Created by light on 19-10-31.
//
#include "array.h"
class Student {
private:
string name;
int score;
public:
Student(string name = "", int score = 0) {
this->name = name;
this->score = score;
}
friend ostream &operator<<(ostream &out, Student &student) {
out << "name : " << student.name << ", " << " score : " << student.score;
return out;
}
bool operator==(const Student &student) {
return this->name == student.name && this->score == student.score;
}
};
class A {
private:
string name;
int age;
void init(string name, int age) {
this->name = name;
this->age = age;
}
public:
A(string name, int age) {
init(name, age);
}
A() {
init("bob",20);
}
void print() {
cout << name << " " << age << endl;
}
// ...
// ...
};
// 测试文件
int main() {
Array<int> array;
for (int i = 0; i < 10; i++) array.addLast(i);
cout << array;
array.add(1, 100);
cout << array;
array.addFirst(-1);
cout << array;
cout << array.get(5) << endl;
array.set(0, 10);
cout << array.get(0) << endl;
cout << array;
cout << array.contains(11) << endl;
cout << array.find(8) << endl; // index=10
array.remove(10);
cout << array.find(8) << endl; // not found
cout << array;
array.removeFirst();
cout << array;
array.removeLast();
cout << array;
array.removeElement(100);
cout << array;
Array<Student> *array1 = new Array<Student>(20);
array1->addLast(Student("Alice", 100));
array1->addLast(Student("Bob", 66));
array1->addLast(Student("Charlie", 88));
cout << *array1;
array1->removeElement(Student("Bob", 66));
cout << *array1;
// array1->~Array();
delete array1;
return 0;
}