Live2D

实现一个栈类,类似STL中的栈

1、思路讲解

stack集合类是一个简单的堆栈的实现。

这里有两个模板参数,T和size,T用于指定堆栈中的元素类型,my_size用于表示堆栈中项数的最大值。

类中添加方法isempty、isfull、push、pop。

2、涉及解说

  对于很久没用C++写代码的我,对于模板类很陌生了,所以首先简单介绍下模板类。

(1)用途:有那么一部分类,用途和方法是一样的,只是涉及的一些参数不同(如参数类型),这个时候引入了类模板来解决这个问题;有了类模板,我们在声明类的时候对于参数的不确定,我们先不给于具体的绑定,等到实例化的时候再指明具体的性质。

  例如函数模板的swap函数,有的想实现int型的两个变量值交换,有的想实现两个string型变量值的交换;有了函数模板,我们只需要写一个函数就可以解决不同需求:

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 template<typename T>
 6 void mySwap(T &a,T &b)
 7 {
 8     T temp;
 9     temp = a;
10     a = b;
11     b = temp;
12 }
13 
14 int main()
15 {
16     //prepare parameter
17     int a1=3,b1=4;
18     string a2="Christal",b2="Carl";
19     //swap two int parameter
20     cout<<"a1= "<<a1<<" b1= "<<b1<<endl;
21     mySwap<int>(a1,b1);
22     cout<<"a1= "<<a1<<" b1= "<<b1<<endl;
23     //swap two string parameter
24     cout<<endl<<"a2= "<<a2<<" b2= "<<b2<<endl;
25     mySwap<string>(a2,b2);
26     cout<<"a2= "<<a2<<" b2= "<<b2<<endl;
27 
28     return 0;
29 }

  输出结果:

(2)用法:

1 template<class 模板参数表>
2 class 类名
3 {
4     //类定义
5 };
6 int main()
7 {
8     类名<参数类型> 对象名;
9 }

  其中,template是类模板声明的关键字;模板参数可以只有一个,也可以有多个;参数可以是类型参数也可以是非类型参数;类型参数用关键字class或typename;非类型参数由一个普通参数构成,代表模板定义中的一个常量。

 1 template<class type,int width> class Hey;
 2 //type为类型参数,width为非类型参数

(3)类模板的实例化

  type、width是形参,同类型的实参值被提供给形参;指定的每个不同类型的值都创建一个新类。

1 template<class type,int width>
2 class Hey
3 {
4 private:
5     type str;
6     int maxwidth;
7 public:
8     Hey():maxwidth(width){}
9 };

  type被指定为string,width被指定为10,创建一个类;

1 Hey<string,10> say1;

  type被指定为char,width被指定为1,创建一个类;

1 Hey<char,1> say2;

3、思路实现

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 template<class T,int my_size> //T:type , my_size:size
 6 class myStack
 7 {
 8 private:
 9     int top; //top pointer
10     T items[my_size]; //stack array
11     const int max_size; //array size
12 public:
13     myStack():max_size(my_size),top(-1){}
14     bool isempty();
15     bool isfull();
16     void push(const T temp);
17     T pop();
18 };
19 template<class T,int my_size>
20 bool myStack<T,my_size>::isempty()
21 {
22     if(top == -1)
23         return true;
24     else
25         return false;
26 }
27 template<class T,int my_size>
28 bool myStack<T,my_size>::isfull()
29 {
30     if(top == max_size-1)
31         return true;
32     else
33         return false;
34 }
35 template<class T,int my_size>
36 void myStack<T,my_size>::push(const T temp)
37 {
38     if(!isfull())
39     {
40         items[++top] = temp;
41     }
42 }
43 template<class T,int my_size>
44 T myStack<T,my_size>::pop()
45 {
46     if(!isempty())
47     {
48         return items[top--];
49     }
50 }
51 
52 int main()
53 {
54     //prepare parameter
55     int i=0;
56     string x;
57     string temp[] = {"Christal","Carl","Jerry","Belle","Sea","vinky","Rita","Nila"};
58     //initialize stack
59     myStack<string,5> s;
60     //push someone
61     while(!s.isfull())
62     {
63         s.push(temp[i]);
64         cout<<"push one person"<<endl;
65         i++;
66     }
67     cout<<endl<<"stack is full"<<endl<<endl;
68     //pop someone
69     while(!s.isempty())
70     {
71         x = s.pop();
72         cout<<x<<" is pop"<<endl;
73     }
74     cout<<endl<<"stack is empty"<<endl;
75 
76     return 0;
77 }

  输出检验: 

posted @ 2017-07-18 17:20  Christal_R  阅读(853)  评论(0编辑  收藏  举报