support bazel complie this project and format code.
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
#include<map>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
string name[]={"张三","李四","王麻子"};
|
||||
double salary[]={1200,2000,1450};
|
||||
map<string,double>sal;
|
||||
map<string,double>::iterator p;
|
||||
for(int i=0;i<3;i++){
|
||||
sal.insert(make_pair(name[i],salary[i]));
|
||||
}
|
||||
sal["tom"]=6156;
|
||||
sal["bob"]=5999;
|
||||
for(p=sal.begin();p!=sal.end();p++){
|
||||
cout<<p->first<<"\t"<<p->second<<endl;
|
||||
}
|
||||
string person;
|
||||
cout<<"输入查找人员的姓名:";
|
||||
cin>>person;
|
||||
int flag=1;
|
||||
for(p=sal.begin();p!=sal.end();p++)
|
||||
if(p->first==person){
|
||||
cout<<p->second<<endl;
|
||||
flag=0;
|
||||
}
|
||||
if(flag)
|
||||
cout<<"没查找到对应的结果!"<<endl;
|
||||
|
||||
cout<<"输入待删除的人员的姓名:";
|
||||
cin>>person;
|
||||
map<string,double>::iterator it;
|
||||
it = sal.find(person);
|
||||
if(it!=sal.end()){
|
||||
cout<<"查找成功:"<<(*it).first<<":"<<(*it).second<<endl;
|
||||
sal.erase(it);
|
||||
cout<<"删除成功"<<endl;
|
||||
}
|
||||
cout<<"删除后的结果为"<<endl;
|
||||
for(p=sal.begin();p!=sal.end();p++){
|
||||
cout<<p->first<<p->second<<endl;
|
||||
}
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
23
practical_exercises/10_day_practice/day8/func/BUILD
Normal file
23
practical_exercises/10_day_practice/day8/func/BUILD
Normal file
@@ -0,0 +1,23 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day8/func:main`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day8/func:max_spec`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day8/func:max`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day8/func:sort`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "main",
|
||||
srcs = ["main.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "max_spec",
|
||||
srcs = ["max_spec.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "max",
|
||||
srcs = ["max.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "sort",
|
||||
srcs = ["sort.cpp"],
|
||||
)
|
||||
@@ -1,4 +1,5 @@
|
||||
//Eg9-1.cpp
|
||||
/* 重要.cpp */
|
||||
// Eg9-1.cpp
|
||||
#include <iostream>
|
||||
//注意一点,max与min使用的时候,容易引起冲突,如果写了下面这一行代码,则要改变函数模板名字,否则直接使用std::cout与std::endl
|
||||
using namespace std;
|
||||
@@ -6,18 +7,12 @@ using namespace std;
|
||||
不要把这里的class与类的声明关键字class混淆在一起,虽然它们由相同的字母组成,但含义是不同的。
|
||||
这里的class表示T是一个类型参数,可以是任何数据类型,如int、float、char等,或者用户定义的struct、enum或class等自定义数据类型。
|
||||
*/
|
||||
template <class T>
|
||||
T Min(T a,T b) {
|
||||
return (a<b)?a:b;
|
||||
}
|
||||
template <class T> T Min(T a, T b) { return (a < b) ? a : b; }
|
||||
/*
|
||||
为了区别类与模板参数中的类型关键字class,标准C++提出?了用typename作为模板参数的类型关键字,同时也支持使用class。
|
||||
比如,把min定义的template <class T>写成下面的形式是完全等价的:
|
||||
*/
|
||||
template <typename T>
|
||||
T myMin(T a, T b){
|
||||
return (a<b)?a:b;
|
||||
}
|
||||
template <typename T> T myMin(T a, T b) { return (a < b) ? a : b; }
|
||||
|
||||
/*
|
||||
模板实例化发生在调用模板函数时。当编译器遇到程序中对函数模板的调用时,
|
||||
@@ -26,18 +21,18 @@ T myMin(T a, T b){
|
||||
当多次发生类型相同的参数调用时,只在第1次进行实例化。编译器只在第1次调用时生成模板函数,
|
||||
当之后遇到相同类型的参数调用时,不再生成其他模板函数,它将调用第1次实例化生成的模板函数。
|
||||
*/
|
||||
int main(){
|
||||
double a=2,b=3.4;
|
||||
float c=2.3,d=3.2;
|
||||
cout<<"2,3 的最小值是:"<<Min<int>(2,3)<<endl; //显式调用
|
||||
cout<<"2,3.4 的最小值是:"<<Min(a,b)<<endl;//隐式调用
|
||||
cout<<"'a','b' 的最小值是:"<<Min('a','b')<<endl;
|
||||
cout<<"2.3,3.2的最小值是:"<<Min(c,d)<<endl;
|
||||
cout<<"2.3,3.2的最大值是:"<<std::min(c,d)<<endl;//引用命名空间内部的最小值函数
|
||||
cout<<"2.3,3.2的最小值是:"<<myMin(c,d)<<endl;//更换class为typename
|
||||
// cout<<"2,'a' 的最小值是:"<<Min(2,'a')<<endl; //报错,不同类型无法处理,请看9-3-1.cpp
|
||||
system("pause");
|
||||
return 0;
|
||||
int main() {
|
||||
double a = 2, b = 3.4;
|
||||
float c = 2.3, d = 3.2;
|
||||
cout << "2,3 的最小值是:" << Min<int>(2, 3) << endl; //显式调用
|
||||
cout << "2,3.4 的最小值是:" << Min(a, b) << endl; //隐式调用
|
||||
cout << "'a','b' 的最小值是:" << Min('a', 'b') << endl;
|
||||
cout << "2.3,3.2的最小值是:" << Min(c, d) << endl;
|
||||
cout << "2.3,3.2的最大值是:" << std::min(c, d)
|
||||
<< endl; //引用命名空间内部的最小值函数
|
||||
cout << "2.3,3.2的最小值是:" << myMin(c, d) << endl; //更换class为typename
|
||||
// cout<<"2,'a' 的最小值是:"<<Min(2,'a')<<endl;
|
||||
// //报错,不同类型无法处理,请看9-3-1.cpp
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
18
practical_exercises/10_day_practice/day8/func/max.cpp
Normal file
18
practical_exercises/10_day_practice/day8/func/max.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
/* 求最大值.cpp */
|
||||
// Eg9-2.cpp
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
template <class T> T Max(T a, T b) { return (a > b) ? a : b; }
|
||||
/*
|
||||
C++在实例化函数模板的过程中,只是简单地将模板参数替换成调用实参的类型,并以此生成模板函数,不会进行参数类型的任何转换。
|
||||
*/
|
||||
int main() {
|
||||
double a = 2, b = 3.4;
|
||||
float c = 5.1, d = 3.2;
|
||||
//在模板调用时进行参数类型的强制转换
|
||||
cout << "2, 3.2 的最大值是:" << Max(double(2), 3.2) << endl;
|
||||
cout << "a, c 的最大值是:" << Max(float(a), c) << endl;
|
||||
//显示指定函数模板实例化的参数类型
|
||||
cout << "'a', 3 的最大值是:" << Max<int>('a', 3) << endl;
|
||||
|
||||
}
|
||||
30
practical_exercises/10_day_practice/day8/func/max_spec.cpp
Normal file
30
practical_exercises/10_day_practice/day8/func/max_spec.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
/* 模板特化.cpp */
|
||||
// Eg9-6.cpp
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
template <class T> T Max(T a, T b) { return (a > b) ? a : b; }
|
||||
//特化
|
||||
// template <> 返回类型 函数名<特化的数据类型>(参数表) {}
|
||||
template <> const char *Max<const char *>(const char *a, const char *b) {
|
||||
return (strcmp(a, b) >= 0) ? a : b;
|
||||
}
|
||||
template <> char *Max<char *>(char *a, char *b) {
|
||||
return (strcmp(a, b) >= 0) ? a : b;
|
||||
}
|
||||
int main() {
|
||||
float c = 5.1, d = 3.2;
|
||||
char s1[] = "xyce", s2[] = "xbv";
|
||||
cout << "2,3的最大值是:" << Max(3, 2) << endl;
|
||||
cout << "c,d的最大值是:" << Max(c, d) << endl;
|
||||
cout << Max("xbv", "xyce") << endl;
|
||||
cout << Max(s1, s2) << endl;
|
||||
|
||||
}
|
||||
/*
|
||||
① 当程序中同时存在模板和它的特化时,特化将被优先调用;
|
||||
②
|
||||
在同一个程序中,除了函数模板和它的特化外,还可以有同名的普通函数。其区别在于C++会对普通函数的调用实参进行隐式的类型转换,
|
||||
但不会对模板函数及特化函数的参数进行任何形式的类型转换。
|
||||
|
||||
*/
|
||||
33
practical_exercises/10_day_practice/day8/func/sort.cpp
Normal file
33
practical_exercises/10_day_practice/day8/func/sort.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/* 例1.cpp */
|
||||
//函数模板参数可以是类属参数,也可以包括普通类型的参数
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
template <class T>
|
||||
//实现降序
|
||||
void sort(T *a, int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
int p = i;
|
||||
for (int j = i; j < n; j++)
|
||||
if (a[p] < a[j])
|
||||
p = j;
|
||||
T t = a[i];
|
||||
a[i] = a[p];
|
||||
a[p] = t;
|
||||
}
|
||||
}
|
||||
template <class T> void display(T &a, int n) {
|
||||
for (int i = 0; i < n; i++)
|
||||
cout << a[i] << "\t" << endl;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
int a[] = {1, 41, 2, 5, 8, 21, 23};
|
||||
char b[] = {'a', 'x', 'y', 'e', 'q', 'g', 'o', 'u'};
|
||||
sort(a, 7);
|
||||
sort(b, 8);
|
||||
display(a, 7);
|
||||
display(b, 8);
|
||||
|
||||
return 0;
|
||||
}
|
||||
8
practical_exercises/10_day_practice/day8/stl/BUILD
Normal file
8
practical_exercises/10_day_practice/day8/stl/BUILD
Normal file
@@ -0,0 +1,8 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day8/stl:map`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "map",
|
||||
srcs = ["map.cpp"],
|
||||
)
|
||||
47
practical_exercises/10_day_practice/day8/stl/map.cpp
Normal file
47
practical_exercises/10_day_practice/day8/stl/map.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
string name[] = {"张三", "李四", "王麻子"};
|
||||
double salary[] = {1200, 2000, 1450};
|
||||
map<string, double> sal;
|
||||
map<string, double>::iterator p;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
sal.insert(make_pair(name[i], salary[i]));
|
||||
}
|
||||
sal["tom"] = 6156;
|
||||
sal["bob"] = 5999;
|
||||
for (p = sal.begin(); p != sal.end(); p++) {
|
||||
cout << p->first << "\t" << p->second << endl;
|
||||
}
|
||||
string person;
|
||||
cout << "输入查找人员的姓名:";
|
||||
cin >> person;
|
||||
int flag = 1;
|
||||
for (p = sal.begin(); p != sal.end(); p++)
|
||||
if (p->first == person) {
|
||||
cout << p->second << endl;
|
||||
flag = 0;
|
||||
}
|
||||
if (flag)
|
||||
cout << "没查找到对应的结果!" << endl;
|
||||
|
||||
cout << "输入待删除的人员的姓名:";
|
||||
cin >> person;
|
||||
map<string, double>::iterator it;
|
||||
it = sal.find(person);
|
||||
if (it != sal.end()) {
|
||||
cout << "查找成功:" << (*it).first << ":" << (*it).second << endl;
|
||||
sal.erase(it);
|
||||
cout << "删除成功" << endl;
|
||||
}
|
||||
cout << "删除后的结果为" << endl;
|
||||
for (p = sal.begin(); p != sal.end(); p++) {
|
||||
cout << p->first << p->second << endl;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
//函数模板参数可以是类属参数,也可以包括普通类型的参数
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
template <class T>
|
||||
//实现降序
|
||||
void sort(T *a, int n){
|
||||
for (int i=0;i<n;i++){
|
||||
int p=i;
|
||||
for (int j=i;j<n;j++)
|
||||
if(a[p]<a[j])
|
||||
p=j;
|
||||
T t=a[i];
|
||||
a[i]=a[p];
|
||||
a[p]=t;
|
||||
}
|
||||
}
|
||||
template <class T>
|
||||
void display(T& a,int n) {
|
||||
for(int i=0;i<n;i++)
|
||||
cout<<a[i]<<"\t"<<endl;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int a[]={1,41,2,5,8,21,23};
|
||||
char b[]={'a','x','y','e','q','g','o','u'};
|
||||
sort(a,7);
|
||||
sort(b,8);
|
||||
display(a,7);
|
||||
display(b,8);
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
//Eg9-6.cpp
|
||||
#include <iostream>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
template <class T>
|
||||
T Max(T a,T b) {
|
||||
return (a>b)?a:b;
|
||||
}
|
||||
//特化
|
||||
//template <> 返回类型 函数名<特化的数据类型>(参数表) {}
|
||||
template<>const char * Max<const char *>(const char *a,const char *b) {
|
||||
return (strcmp(a,b)>=0)?a:b;
|
||||
}
|
||||
template<>char * Max<char *>(char *a,char *b) {
|
||||
return (strcmp(a,b)>=0)?a:b;
|
||||
}
|
||||
int main(){
|
||||
float c=5.1,d=3.2;
|
||||
char s1[]="xyce", s2[]="xbv";
|
||||
cout<<"2,3的最大值是:"<<Max(3,2)<<endl;
|
||||
cout<<"c,d的最大值是:"<<Max(c,d)<<endl;
|
||||
cout<<Max("xbv","xyce")<<endl;
|
||||
cout<<Max(s1,s2)<<endl;
|
||||
system("pause");
|
||||
}
|
||||
/*
|
||||
① 当程序中同时存在模板和它的特化时,特化将被优先调用;
|
||||
② 在同一个程序中,除了函数模板和它的特化外,还可以有同名的普通函数。其区别在于C++会对普通函数的调用实参进行隐式的类型转换,
|
||||
但不会对模板函数及特化函数的参数进行任何形式的类型转换。
|
||||
|
||||
*/
|
||||
@@ -1,21 +0,0 @@
|
||||
//Eg9-2.cpp
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
template <class T>
|
||||
T Max(T a,T b) {
|
||||
return (a>b)?a:b;
|
||||
}
|
||||
/*
|
||||
C++在实例化函数模板的过程中,只是简单地将模板参数替换成调用实参的类型,并以此生成模板函数,不会进行参数类型的任何转换。
|
||||
*/
|
||||
int main(){
|
||||
double a=2,b=3.4;
|
||||
float c=5.1,d=3.2;
|
||||
//在模板调用时进行参数类型的强制转换
|
||||
cout<<"2, 3.2 的最大值是:"<<Max(double(2),3.2)<<endl;
|
||||
cout<<"a, c 的最大值是:"<<Max(float(a),c)<<endl;
|
||||
//显示指定函数模板实例化的参数类型
|
||||
cout<<"'a', 3 的最大值是:"<<Max<int>('a',3)<<endl;
|
||||
system("pause");
|
||||
}
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
设计一个堆栈的类模板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;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
//设计一通用数组类,它能够直接存取数组元素,并能够对数组进行从大到小的排序。
|
||||
|
||||
#include<iostream>
|
||||
#include<cstring>
|
||||
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;
|
||||
system("pause");
|
||||
}
|
||||
Reference in New Issue
Block a user