关键字explict、template<class T>

来源:互联网 发布:ubuntu映射网络文件夹 编辑:程序博客网 时间:2024/05/28 05:13

题目名称 : [Exception] Stack and Exception

时间限制 : 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 failedpop 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;
    }
    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;

原创粉丝点击