C++学习日志之运用类模板正确使用指针栈

由于指针栈的特殊性,模板类中需要包含析构函数,复制构造函数,复制运算符等。
student.h
#ifndef STUENT_H_
#define STUDENT_H_
template <class Type>
class Stack{
private:
    enum{MAX=10};
    int stacksize;
    Type *item;
    int top;
public:
    explicit Stack(int ss=MAX);
    Stack(const Stack &st);
    ~Stack() {delete [] item;}

    bool isempty() {return top==0;}
    bool isfull()  {return top==MAX;}
    bool push(const Type &item);
    bool pop(Type & item);
    Stack & operator = (const Stack &st);
};
template <class Type>
Stack<Type>::Stack(int ss):stacksize(ss),top(0)
{
    item = new Type [stacksize];
}
template <class Type>
Stack<Type>::Stack(const Stack &st)
{
    stacksize = st.stacksize;
    top=st.top;
    item=new Type [st.stacksize];
    for(int i=0;i<stacksize;i++)
        item[i]=st.item[i];
}
template <class Type>
bool Stack<Type>::push(const Type & items)
{
    if(top<stacksize)
    {
        item[top++]=items;
        return true;
    }
    else
        return false;
}
template <class Type>
bool Stack<Type>::pop(Type & items)
{
    if(top>0)
    {
        items=item[--top];
        return true;
    }
    else
        return false;
}
template <class Type>
Stack<Type> & Stack<Type>::operator = (const Stack<Type> & st)
{
    if(this==&st)
        return *this;
    delete [] item;
    stacksize = st.stacksize;
    top=st.top;
    item = new Type [st.stacksize];
    for(int i=0;i<stacksize;i++)
        item[i]=st.item[i];
        return *this;
}
#endif // STUENT_H_


main.cpp

#include<iostream>

#include"student.h"
#include<cstdlib>
#include<ctime>
using std::cin;
using std::cout;
using std::endl;
const int Num =10;

int main()
{
   srand(time(0));
   cout << "please enter stack size: ";
   int stacksize;
   cin >> stacksize;
   Stack <const char *> st(stacksize);
   const char * in[Num] = {
                            "1:hank gilgamesh","2:kiki ishtar","3:betty rocker","4:ian flagranti","5:wolfgang kibble",
                            "6:portia koop","7:joy almondo","8:xaverie paprika","9:juan moore","10:misha mache"
                          };
    const char *out[Num];
    int processed = 0;
    int nextin =0;
    while(processed<stacksize)
    {
        if(st.isempty())
            st.push(in[nextin++]);
        else if(st.isfull())
            st.pop(out[processed++]);
        else if(rand()%2&&nextin<Num)
            st.push(in[nextin++]);
        else
            st.pop(out[processed++]);
    }
    for(int i=0;i<stacksize;i++)
        cout << out[i] << endl;
    cout << "bye.";
    return 0;
}


运行结果:

                                    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
环境:Windows XP S3、VC++ 6.o 目的:学习C++程序开发语言 使用步骤:下载之后,双击.dsw文件即可打开该示例工程 说明: 在学习《Thinking in C++》一书关于数组声明与定义时,Bruce说如果这样声明一个数组: int b[6] = {0}; Here, the compiler will use the first initializer for the first array element, and then use zero for all the elements without initializers.(意思是说如果这样声明并且定义一个数组,那么编译器会把0赋给第一个数组元素,其它五个元素会赋值0).于是我使用class声明一个类型Test。在这个类中有一个成员方法叫getArray(),在该方法中使用以上方式声明一个数组,然后返回数组的指针,然后在另一个成员方法showPointerOfArray(int*)接收传过来的int指针,在这个方法操作数组。 但是在运行时没有出现我想要的结果,于是其它的方法中测试这样声明方式,却是运行正确的。于是让我很纳闷?带这个问题与本中心庄鹏飞老师讨论之后,发现原来我没有搞清楚在C++指针分为指针和堆指针。参见int* Test::getArray()方法中关于数组的声明以及本人非常详细的说明,那么我想会给学习C++编程的人员带来收获。 结论:C++不是纯粹的OO语言,这是bruce说的。本人在学习过程中确实感觉C++这种语言比Java难得多。不像Java那么直观易学,这可能也就是为什么世界上所有程序员中有20%左右的人是Java程序员,而不是C++程序员的原因吧。 另外,本人使用QT的g++编译器编译通过了,因为是使用记事本手写的,所以完全是Java的书写风格^_^ 把它搞成VC++的工程是为了大家方便学习。。。 学习对象:希望编写效率高于Java应用的程序员。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值