Template:<bits/_stack.h>

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.