#include
using namespace std;
template
class stack {
public:
stack(T n)
: base(new T[n]), sp(base), size(0), capacity(n)
{ }
T push(T n) {
*sp++ = n;
++size;
return n;
}
T pop() {
return *--sp;
}
void dump() const {
for(T* p = base; p != sp; ++p)
cout << *p << ' ';
cout.put('\n');
}
bool empty() const {
return sp == base;
}
~stack() {
if(capacity != 0)
delete [] base;
}
private:
T* base;
T* sp;
int size;
const int capacity;
};
#define PUSH( s, a, b ) for( i = 0; i < b; ++i ) s.push( a+i );
#define POP( a ) while( !a.empty() ) cout << a.pop() << ' '; cout.put( '\n' );
int main()
{
stack
stack
stack
int i;
PUSH( a, 1, 3 );
PUSH( b, 'a', 5 );
PUSH( c, 1.1, 8 );
a.dump(); b.dump(); c.dump();
POP( a ); POP( b ); POP( c );
return 0;
}
附图: