-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
array_template.cpp
80 lines (74 loc) · 1.42 KB
/
array_template.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
/* 类模板特化之数组.cpp */
#include <cstring>
#include <iostream>
using namespace std;
#define MAXSIZE 5
template <class T> class Array {
public:
Array() {
for (int i = 0; i < MAXSIZE; i++) {
array[i] = 0;
}
}
T &operator[](int i);
void Sort();
private:
T array[MAXSIZE];
};
template <class T> T &Array<T>::operator[](int i) {
if (i < 0 || i > MAXSIZE - 1) {
cout << "数组下标越界!" << endl;
exit(0);
}
return array[i];
}
template <class T> void Array<T>::Sort() {
int p, j;
for (int i = 0; i < MAXSIZE - 1; i++) {
p = i;
for (j = i + 1; j < MAXSIZE; j++) {
if (array[p] < array[j])
p = j;
}
T t;
t = array[i];
array[i] = array[p];
array[p] = t;
}
}
template <> void Array<char *>::Sort() {
int p, j;
for (int i = 0; i < MAXSIZE - 1; i++) {
p = i;
for (j = i + 1; j < MAXSIZE; j++) {
if (strcmp(array[p], array[j]) < 0)
p = j;
}
char *t = array[i];
array[i] = array[p];
array[p] = t;
}
}
int main(int argc, char const *argv[]) {
Array<int> a1;
Array<char *> b1;
a1[0] = 1;
a1[1] = 23;
a1[2] = 6;
a1[3] = 3;
a1[4] = 9;
a1.Sort();
for (int i = 0; i < 5; i++)
cout << a1[i] << "\t";
cout << endl;
b1[0] = "x1";
b1[1] = "ya";
b1[2] = "ad";
b1[3] = "be";
b1[4] = "bc";
b1.Sort();
for (int i = 0; i < 5; i++)
cout << b1[i] << "\t";
cout << endl;
return 0;
}