My c ++ practices (1): stack implementation

Source: Internet
Author: User

(1) Basic Stack: Use STD: deque as the internal container. Methods include inbound, outbound, back to the top element of the stack, and judge whether the stack is empty.

// Stack1.h: basic stack implementation <br/> # ifndef _ stack1_h __< br/> # DEFINE _ stack1_h __< br/> # include <deque> <br/> # include <stdexcept> <br/> template <typename T> <br/> class Stack {<br/> PRIVATE: <br/> STD: deque <t> elems; // container containing elements <br/> Public: <br/> void push (T const &); // press the element <br/> void POP (); // the pop-up element <br/> T top () const; // return the top element of the stack <br/> bool empty () const {// determine whether the stack is empty <br/> return elems. empty (); <br/>}< br/>}; <br/> template <typename T> <br/> void stack <t> :: push (T const & ELEM) {<br/> elems. push_back (ELEM); // Add an element copy to the end <br/>}< br/> template <typename T> <br/> void stack <t> :: pop () {<br/> If (elems. empty () {<br/> throw STD: out_of_range ("Stack <t>: Pop (): Empty stack "); <br/>}< br/> elems. pop_back (); // delete the end element <br/>}< br/> template <typename T> <br/> T stack <t>: Top () const {<br/> If (elems. empty () {<br/> throw STD: out_of_range ("Stack <t>: Top (): Empty stakc "); <br/>}< br/> return elems. back (); // returns a copy of the End Element <br/>}< br/> # endif

Test procedure:

// Stack1test. CPP: test stack <br/> # include <iostream> <br/> # include <string> <br/> # include <cstdlib> <br/> # include "stack1.h" <br /> int main () {<br/> try {<br/> stack <int> intstack; // stack whose element type is int <br/> stack <STD: String> stringstack; // stack whose element type is string <br/> intstack. push (7); <br/> STD: cout <intstack. top () <STD: Endl; <br/> stringstack. push ("hello"); <br/> STD: cout <stringstack. top () <STD: Endl; <br/> stringstack. pop (); <br/> stringstack. pop (); // throw an STD: out_of_range exception <br/>} catch (STD: exception const & Ex) {// capture and print the exception <br/> STD: cerr <"exception:" <ex. what () <STD: Endl; <br/> return exit_failure; <br/>}< br/>}

(2) improvement 1: Specify the stack capacity and use non-type template parameters. Because the stack capacity is fixed, you can use arrays to store elements.

// Stack2.h: stack with limited capacity <br/> # ifndef _ stack2_h __< br/> # DEFINE _ stack2_h __< br/> # include <stdexcept> <br/> template <typename t, int size> <br/> class Stack {<br/> T elems [size]; // an array can be used to manage elements due to limited capacity. <br/> int numelems; // The current total number of elements <br/> Public: <br/> stack (); // constructor <br/> void push (T const &); // press the element <br/> void POP (); // the pop-up element <br/> T top () const; // return the top element of the stack <br/> bool empty () const {// determine whether the stack is empty <br/> return numelems = 0; <br/>}< br/> bool full () const {// determine whether the stack is full <br/> return numelems = size; <br/>}< br/>}; <br/> template <typename T, int size> <br/> stack <t, size>: Stack (): numelems (0) {// elements not included in the initial stage <br/> // No Action <br/>}< br/> template <typename t, int size> <br/> void stack <t, size>: Push (T const & ELEM) {<br/> If (numelems = size) <br/> throw STD: out_of_range ("Stack <t, size >:: push (): Stack is full"); <br/> elems [numelems] = ELEM; // additional element <br/> + numelems; // Add the number of elements <br/>}< br/> template <typename t, int size> <br/> void stack <t, size>: Pop () {<br/> If (numelems <= 0) <br/> throw STD :: out_of_range ("Stack <t, size >:: POP (): Empty stack"); <br/> -- numelems; // reduce the number of elements <br/>}< br/> template <typename T, int size> <br/> T stack <t, size >:: top () const {<br/> If (numelems <= 0) <br/> throw STD: out_of_range ("Stack <>:: top (): Empty stack "); <br/> return elems [numElems-1]; // return the end element <br/>}< br/> # endif

Test procedure:

// Stack2test. CPP: stack with limited test capacity <br/> # include <iostream> <br/> # include <string> <br/> # include <cstdlib> <br/> # include "stack2.h "<br/> int main () {<br/> try {<br/> stack <int, 20> int20stack; // stack that can store 20 int elements <br/> stack <int, 40> int40stack; <br/> stack <STD: String, 40> stringstack; <br/> int20stack. push (7); <br/> STD: cout <int20stack. top () <STD: Endl; <br/> int20stack. pop (); <br/> stringstack. push ("hello"); <br/> STD: cout <stringstack. top () <STD: Endl; <br/> stringstack. pop (); <br/> stringstack. pop (); <br/>}catch (STD: exception const & Ex) {<br/> STD: cerr <"exception:" <ex. what () <STD: Endl; <br/> return exit_failure; <br/>}< br/> return 0; <br/>}

(3) Improvement 2: You can specify the containers used inside the stack, and assign values between different types of stacks. Template parameters are required. To define a member template with a value assignment operator, it does not overwrite the default value assignment operator. For values assigned between stacks of the same type, the default value assignment operator is still called. The container has two template parameters, one of which is the element type and the other is the Distributor Type. Therefore, when defining the template parameter of the internal container of the stack, both template parameters are required. If only one element type parameter is defined, it cannot be matched when you pass in the container (such as vector and deque. In short, the template parameters we define must exactly match the template we passed in.

// Stack3.hpp: You can specify an internal container, and stacks of different types can be assigned values <br/> # ifndef stack_hpp <br/> # define stack_hpp <br/> # include <deque> <br/> # include <stdexcept> <br/> # include <memory> <br/> template <typename t, template <typename ELEM, typename alloc = STD: Allocator <ELEM >>< br/> class cont = STD :: deque> <br/> class Stack {<br/> cont <t> elems; // window for storing elements <br/> public: <br/> void push (T const &); // The push element <br/> void POP (); // The pop element <br/> T OP () const; // return the top element of the stack <br/> bool empty () const {// determine whether the stack is empty <br/> return elems. empty (); <br/>}< br/>/* This assignment operator is an independent member template, which can set the stack <t2, cont2 <elem2> type stack assignment stack <t, cont <ELEM> type stack, <br/> As long as elem2 type can be implicitly converted to ELEM type. It does not overwrite the default value operator. For assignments between stacks of the same type, the default value assignment operator <br/> */<br/> template <typename T2, template <typename elem2, typename alloc = STD: Allocator <elem2> <br/> class cont2> <br/> stack <t, cont> & operator = (stack <t2, cont2> const &); <br/>}; <br/>/* Because ELEM and alloc parameters are not required, therefore, you can omit */<br/> template <typename T, template <typename, typename> class cont> <br/> void stack <t, cont> :: push (T const & ELEM) {<br/> elems. push_back (ELEM ); // Add an element copy to the end <br/>}< br/> template <typename T, template <typename, typename> class cont> <br/> void stack <t, cont>: Pop () {<br/> If (elems. empty () <br/> throw STD: out_of_range ("Stack <>: Pop (): Empty stack"); <br/> elems. pop_back (); // delete the end element <br/>}< br/> template <typename T, template <typename, typename> class cont> <br/> T stack <t, cont>: Top () const {<br/> If (elems. empty () <br/> throw STD: out_of_range ("s Tack <>:: top (): Empty stack "); <br/> return elems. back (); // returns a copy of the End Element <br/>}< br/> template <typename T, template <typename, typename> class cont> <br/> template <typename T2, template <typename, typename> class cont2> <br/> stack <t, cont> & stack <t, cont>: Operator = (stack <t2, cont2> const & RHs) {<br/> If (void *) This = (void *) & RHs) // if the value is self-assigned <br/> return * This; <br/> stack <t2, cont2> TMP (RHs); <br/> elems. clear (); // Delete all existing elements <br/> while (! TMP. empty () {<br/> // copies the elements in the RHS stack to the current stack, and the sequence of the elements remains unchanged <br/> elems. push_front (TMP. top (); <br/> TMP. pop (); <br/>}< br/> return * This; <br/>}< br/> # endif

Test procedure:

// Stack3test. CPP: test the improved stack function <br/> # include <iostream> <br/> # include <string> <br/> # include <cstdlib> <br/> # include <Vector> <br/> # include "stack3.hpp" <br/> int main () {<br/> try {<br/> stack <int> intstack; // int stack <br/> stack <float> floatstack; // float stack </P> <p> intstack. push (42); <br/> intstack. push (7); <br/> floatstack. push (7.7); <br/> floatstack = intstack; // value assignment between stacks of different types <br/> STD: cout <floatstack. top () <STD: Endl; <br/> floatstack. pop (); <br/> STD: cout <floatstack. top () <STD: Endl; <br/> floatstack. pop (); <br/> STD: cout <floatstack. top () <STD: Endl; <br/> floatstack. pop (); <br/>}catch (STD: exception const & Ex) {<br/> STD: cerr <"exception:" <ex. what () <STD: Endl; <br/>}< br/> stack <int, STD: vector> vstack; // use vector as the int stack of the internal container <br/> vstack. push (42); <br/> vstack. push (7); <br/> STD: cout <vstack. top () <STD: Endl; <br/> vstack. pop (); <br/> return 0; <br/>}

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.