Today grabbed stack template various changed the pitch ... After a variety of infinite CE finally put this bt template out ...
Also learned a lot about the template of the knowledge-mainly is the structure of static variables to be declared after the outside can be used-or error-
Prototype:
Template<typename T>class Stack;
How to use:
Using namespace Popoqqq_stack;int Main () {<span style= "White-space:pre" ></span>Stack<int> s;< Span style= "White-space:pre" ></span>s.push (1);//into the stack <span style= "White-space:pre" ></span>cout <<s.top () <<endl;//out stack <span style= "White-space:pre" ></span>cout<<s.size () << endl;//returns the size of the stack <span style= "White-space:pre" ></span>cout<<s.empty () <<endl;//returns whether the stack is empty < Span style= "White-space:pre" ></span>s.pop ();//Stack <span style= "White-space:pre" ></span>cout <<s.empty () <<endl;}
Code:
#define _stack_//writen by popoqqq//shouldn ' t access fuction "TOP ()" or "Pop ()" If the STACK is Empty.namespace popoqqq_ Stack{template<typename t>class stack_point{private:static stack_point<t>* Bin;public:T mem; Stack_point *last;void* operator new (size_t,t _,stack_point *__); void operator delete (void *p);}; Template<typename t> stack_point<t>* stack_point<t>:: Bin;template<typename T> void* Stack_ Point<t>:: operator new (size_t,t _,stack_point *__) {if (bin) {Stack_point *re=bin;bin=bin->last;re->mem=_ ; Re->last=__;return re;} Static Stack_point *mempool,*c;if (c==mempool) {c=new stack_point[1<<15];mempool=c+ (1<<15);} c->mem=_; C->last=__;return C + +;} Template<typename t> void stack_point<t>:: operator delete (void *p) {(stack_point<t>*) p)->last= bin;} Template<typename t>class stack{private:stack_point<t> *top;int size;public:stack (): Top (0x0), size (0) {} Inline T Top () {return top->mem;} Inline void Pop () {stack_point<t>* temp=top->last;delete top;top=temp;--size;} inline bool Empty () {return size==0;} inline int size () {return size;} inline void Push (T x) {top=new (x,top) stack_point<t>;++size;}};}
Template:<bits/_stack.h>