关键字explict、template<class T>
2017-06-12 20:56
399 查看
时间限制 : 1000 ms
空间限制 : 32 MB
Description
【The point of problem is exception】Implement the definition of class Stack(last in first out).
const int MAX_LEN = 5; template<class T> class Stack { public: explicit Stack(); bool empty() const; int size() const; T& top(); const T& top() const; void push(const T& x); void pop(); private: T data[MAX_LEN]; int stack_size; };
The max capacity of the Stack is 5. When there are five elements in the Stack, we should follow the rule if we want to push a new element into stack:
Assume the top element of the Stack is b, the new element is a:
1) if a > b, remove the top element and push the new element into Stack
2) if a <= b, do nothing.
when executing operation pop(), it should throw a double exception if there is no element in the Stack. when executing operation top(), it should throw a int exception if there is no element in the Stack. Like the following program:
Stack<int> s; try { s.top(); } catch (int) { cout << "get top failed" << endl; } try { s.pop(); } catch (double) { cout << "pop failed" << endl; }
【Sample Out】
The output should be:
get top failed pop failed
代码:
Stack.h:
#ifndef Stack_and_Exception_Stack_h
#define Stack_and_Exception_Stack_h
const int MAX_LEN = 5;
template<class T>
class Stack {
public:
explicit Stack(): stack_size(0) {}/*关键字explicit,可以阻止不应该允许的经过转换构造函数进行的隐式转换的发生。声明为explicit的构造函数不能在隐式转换中使用。*/
bool empty() const {
return stack_size == 0;
4000
}
int size() const {
return stack_size;
}
T& top();
const T& top() const;
void push(const T& x);
void pop();
operator bool() const {return !empty();}
private:
T data[MAX_LEN];
int stack_size;
};
template<class T>
T& Stack<T>::top() {
if (empty())
throw 1;
return data[stack_size - 1];
}
template<class T>
const T& Stack<T>::top() const {
if (empty())
throw 1;
return data[stack_size - 1];
}
template<class T>
void Stack<T>::pop() {
if (empty())
throw 1.0;
stack_size--;
}
template<class T>//每步实现之前都要加上这一句
void Stack<T>::push(const T& x) {
if (stack_size == MAX_LEN) {
if (!(top() < x))
return;
stack_size--;
}
data[stack_size++] = x;
}
#endif
main.cpp
#include "Stack.h"
#include <iostream>
using namespace std;
int main() {
Stack<int> s;
cout << s.size() << endl;
for (int i = 10; i < 15; i++) {
s.push(i);
cout << s.size() << endl;
}
cout << s.size() << endl;
s.push(12);
cout << s.top() << endl;
cout << s.size() << endl;
s.push(16);
cout << s.top() << endl;
cout << s.size() << endl;
for (int i = 5; i >= 0; i--) {
try {
cout << s.top() << endl;
}
catch(int int_e) {
cout << "get top failed" << endl;
}
try {
s.pop();
}
catch(double dou_e) {
cout << "pop failed" << endl;
}
cout << s.size() << endl;
}
for (int i = 10; i < 15; i++) {
s.push(i);
cout << s.size() << endl;
cout << s.top() << endl;
}
return 0;
}
知识点1、template<class T>
template <class 类型参数名>,template意思是“模板
用类模板定义对象的方法与此相似,但是不能直接写成
Compare cmp(4,7); // Compare是类模板名
Compare是类模板名,而不是一个具体的类,类模板体中的类型numtype并不是一个实际的类型,只是一个虚拟的类型,无法用它去定义对象。必须用实际类型名去取代虚拟的类型,具体的做法是:
Compare <int> cmp(4,7);
eg:
#include <iostream>
using namespace std;
template <class numtype>
//定义类模板
class Compare
{
public :
Compare(numtype a,numtype b)
{x=a;y=b;}
numtype max( )
{return (x>y)?x:y;}
numtype min( )
{return (x<y)?x:y;}
private :
numtype x,y;
};
int main( )
{
Compare<int > cmp1(3,7); //定义对象cmp1,用于两个整数的比较
cout<<cmp1.max( )<<" is the Maximum of two integer numbers."<<endl;
cout<<cmp1.min( )<<" is the Minimum of two integer numbers."<<endl<<endl;
Compare<float > cmp2(45.78,93.6); //定义对象cmp2,用于两个浮点数的比较
cout<<cmp2.max( )<<" is the Maximum of two float numbers."<<endl;
cout<<cmp2.min( )<<" is the Minimum of two float numbers."<<endl<<endl;
Compare<char> cmp3(′a′,′A′); //定义对象cmp3,用于两个字符的比较
cout<<cmp3.max( )<<" is the Maximum of two characters."<<endl;
cout<<cmp3.min( )<<" is the Minimum of two characters."<<endl;
return 0;
} 上面列出的类模板中的成员函数是在类模板内定义的。如果改为在类模板外定义,不能用一般定义类成员函数的形式:
numtype Compare::max( ) {…} //不能这样定义类模板中的成员函数
而应当写成类模板的形式:
template <class numtype>
numtype Compare<numtype>::max( )
{
return (x>y)?x:y;
}
类模板的类型参数可以有一个或多个,每个类型前面都必须加class,如:
template <class T1,class T2>
class someclass
{…};
在定义对象时分别代入实际的类型名,如:
someclass<int,double> obj;
相关文章推荐
- c++ 模板<template class T> c++ 模板<template class T>
- C++中template<class T> && template <typename T>(模板区分)
- template <class T> T * funcNew(T x,int n)
- template<class T>
- c++中的 template<class Type>
- 宏和template<class T>
- c++ error: reference to 'plus' is ambiguous ; template<class _Tp> struct std::plus
- template<class T>
- <转载>模板声明中template <typename T>和template <class T>
- template<class T>
- template<class T>
- template<class T>(转)
- C++类模板 template <class T>详细使用方法
- 类模板 template<class T>
- C++类模板 template <class T>
- template<class T>
- template<class T>函数模板
- C++类模板 template <class T>
- C++类模板 template <class T>
- SeqStack(Template<class T>)实现