我有一个“stack.h”和一个“stack.cpp”文件,它们定义了一个手工制作的堆栈类。
我现在想要做的是创建另一个类“名称”,其中包含一个“nr”堆栈向量,我不知道从哪里开始。
德普.h:
#pragma once
#include <vector>
#include "stiva.h"
template <class T>
class depou {
private:
    int nr;
    std::vector<stiva<T> > depouArray;
public:
    depou (int x, int lungTren);
};
template <class T>
depou<T>::depou (int x, int lungTren){
    int i;
    nr = x;
    depouArray = new std::vector<stiva<T> > (nr);
/*  for (i=0; i<nr; i++){
        depouArray[i] = stiva<T> (lungTren);
    } this is incorrect */
}
我的 name.h 没有编译我只是想知道是否可以做一个这样的向量。
我的堆栈头被称为“stiva”,所以我根据这个编辑了“name.h”文件。
stiva.h
#pragma once
template <class T>
class stiva {
private:
    int top;
    int size;
    T *stackArray;
public:
    stiva();
    stiva (int s);
    ~stiva (){ delete [] stackArray; }
    int push (const T&);
    int pop (T&);
  int topElem (T&);
    int isEmpty (){return top == -1;}
    int isFull () {return top == size -1;}
    int search (T x);
    void setSize (const int& sz){
        size=sz;
    }
};
template <class T>
stiva<T>::stiva (){
    size = 10;
    top = -1;
    stackArray = new T[size];
}
template <class T>
stiva<T>::stiva (int s){
    size = s>0 && s<1000? s : 10;
    top = -1;
    stackArray = new T[size];
}
template <class T>
int stiva<T>::push (const T& x){
    if (!isFull()){
        stackArray[++top] = x;
        return 1;
    }
    else{
        size *= 2;
        stackArray = (T*) realloc (stackArray, 2*size * sizeof(T));
        stackArray[++top] = x;
        return 1;
    }
    return 0;
}
template <class T>
int stiva<T>::pop (T& popValue){
    if (!isEmpty ()){
        popValue = stackArray[top--];
        return 1;
    }
    return 0;
}
template <class T>
int stiva<T>::topElem (T& topValue){
    if (!isEmpty ()){
        topValue = stackArray[top];
        return 1;
    }
    return 0;
}
在主要我初始化是这样的:
德普 d(5,10);