support bazel complie this project and format code.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day8/class_template:stack`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day8/class_template:spec`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "stack",
|
||||
srcs = ["stack.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "spec",
|
||||
srcs = ["spec.cpp"],
|
||||
)
|
@@ -0,0 +1,79 @@
|
||||
/* 类模板特化.cpp */
|
||||
//设计一通用数组类,它能够直接存取数组元素,并能够对数组进行从大到小的排序。
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
const int Size = 5;
|
||||
template <class T> class Array {
|
||||
private:
|
||||
T a[Size];
|
||||
|
||||
public:
|
||||
Array() {
|
||||
for (int i = 0; i < Size; i++) {
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
T &operator[](int i);
|
||||
void Sort();
|
||||
};
|
||||
|
||||
template <class T> T &Array<T>::operator[](int i) {
|
||||
if (i < 0 || i > Size - 1) {
|
||||
cout << "\n数组下标越界!" << endl;
|
||||
exit(1);
|
||||
}
|
||||
return a[i];
|
||||
}
|
||||
|
||||
template <class T> void Array<T>::Sort() {
|
||||
int p;
|
||||
for (int i = 0; i < Size - 1; i++) {
|
||||
p = i;
|
||||
for (int j = i; j < Size; j++) {
|
||||
if (a[p] < a[j])
|
||||
p = j;
|
||||
}
|
||||
T t = a[p];
|
||||
a[p] = a[i];
|
||||
a[i] = t;
|
||||
}
|
||||
}
|
||||
// template <> 返回类型 类模板名<特化的数据类型>::特化成员函数名(参数表){}
|
||||
template <> void Array<char *>::Sort() {
|
||||
int p;
|
||||
for (int i = 0; i < Size - 1; i++) {
|
||||
p = i;
|
||||
for (int j = i + 1; j < Size; j++)
|
||||
if (strcmp(a[p], a[j]) < 0)
|
||||
p = j;
|
||||
char *t = a[p];
|
||||
a[p] = a[i];
|
||||
a[i] = t;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
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;
|
||||
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
/* 模拟栈.cpp */
|
||||
/*
|
||||
设计一个堆栈的类模板Stack,在模板中用类型参数T表示栈中存放的数据,用非类型参数MAXSIZE代表栈的大小。
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
template <class T, int MAXSIZE> class Stack {
|
||||
private:
|
||||
T elem[MAXSIZE];
|
||||
int top;
|
||||
|
||||
public:
|
||||
Stack() { top = 0; };
|
||||
void push(T e);
|
||||
T pop();
|
||||
bool empty() {
|
||||
if (top <= -1)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
void setEmpty() { top = -1; }
|
||||
bool full() {
|
||||
if (top >= MAXSIZE - 1) {
|
||||
return 1;
|
||||
} else
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
/*
|
||||
原型:
|
||||
template <模板参数列表>
|
||||
返回值类型 类模板名<模板参数名表>::成员函数名 (参数列表){};
|
||||
*/
|
||||
template <class T, int MAXSIZE> void Stack<T, MAXSIZE>::push(T e) {
|
||||
if (full()) {
|
||||
cout << "栈已满,不能再添加元素了!";
|
||||
return;
|
||||
}
|
||||
elem[++top] = e;
|
||||
}
|
||||
|
||||
template <class T, int MAXSIZE> T Stack<T, MAXSIZE>::pop() {
|
||||
if (empty()) {
|
||||
cout << "栈已空,不能再弹出元素了!" << endl;
|
||||
return 0;
|
||||
}
|
||||
return elem[top--];
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
//类模板实例化
|
||||
Stack<int, 10> iStack;
|
||||
Stack<char, 10> cStack;
|
||||
iStack.setEmpty();
|
||||
cStack.setEmpty();
|
||||
cout << "-------intStack----\n";
|
||||
int i;
|
||||
for (i = 1; i < 11; i++)
|
||||
iStack.push(i);
|
||||
for (i = 1; i < 11; i++)
|
||||
cout << iStack.pop() << "\t";
|
||||
cout << "\n\n-------charStack----\n";
|
||||
cStack.push('A');
|
||||
cStack.push('B');
|
||||
cStack.push('C');
|
||||
cStack.push('D');
|
||||
cStack.push('E');
|
||||
for (i = 1; i < 6; i++)
|
||||
cout << cStack.pop() << "\t";
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user