Implementation framework of STL stack Adapter

Source: Internet
Author: User

Note: This article is only for learning and communication. For more information, see the source!

I mentionedAdapter)We thought of the modem used to access the Internet over telephone lines in the early days, commonly known as "cat". The role of "cat" is to achieve digital-analog conversion and digital-analog conversion. on the client side, it can convert analog phone information into digital information that our computer can receive, so cat is equivalent to a converter. A better understanding example is provided to illustrate the meaning of "adapter. I believe that there is a plug-in each of our homes. In this case, there is only one triangle plug-in on the walls of our home, while our TV is two. What should I do? There is no doubt that we can connect a new plug-in with at least two holes. One is the plug-in used to connect the triangle on the wall, one is the two plug-ins provided for TV series. In this way, the plug-in used for conversion in the new middle is an adapter.

From the above example, we can easily conclude that:The adapter can encapsulate and modify the original object at a time, and provides another interface, which can better meet the user's needs and has a certain specificity.

In this way, we can introduce the container adapter of stack. We have already introduced three basic sequence containers, namely vector, list, And deque. The design of these three sequence containers is designed with the concept of iterator,STL requires that any container must have a built-in iterator.If the iterator is not built in, the definition of "Container" cannot be discussed.

Everyone who has learned the "Data Structure" knows the stack, which is a typeBack-in-first-outOrAdvanced and laterData structure,Stack insertion and deletion operations and valuesIt can only be performed at the top of the stack. According to the stack nature, the top element of the stack must be the last element to enter. Based on this, we can easily draw a conclusion: Any provided end insert (push_back function), delete (pop_back function), access (back function) can be encapsulated and modified by the stack container. HereEncapsulationIt means that you cannot directly access the encapsulated underlying container through the stack object.ModifyIt refers to the fact that the stack provides the necessaryInterfaceTo meet user needs. We willThe underlying container encapsulated by the adapterCalledBasic container.

In the ordered containers we have learned,Any insert, delete, or access operation at the end can be used as the basic container of the stack adapter.Therefore, vector, list, And deque can be used as the basic container of the stack.The default basic container of stack is deque container..

For a stack, we usually need to provide these interface functions:

(1)Obtains the number of current elements in the stack.: Size_type size ();

(2)Top element of stack (but not pop-up): T top ();

(3)Inbound Stack: Void push (const T & T );

(4)Outbound Stack: Void POP ();

(5)Judge whether the stack is empty: Bool empty ().

The following is the implementation code of the stack adapter:

#include<iostream>#include<deque>using namespace std;
/******************* Stack adapter definition **************/template <class t, class sequence = deque <t> class Stack {friend bool operator = (const stack &, const stack &); // The friend function is a global function, friend bool operator <(const stack &, const stack &); // class internal declaration, out-of-class definition, defined as without friendpublic: typedef typename sequence :: value_type; // typename type declaration, element type typedef typename sequence: size_type; // size type typedef typename sequence: Reference reference; // reference declaration typedef typename sequence :: const_reference; // The constant reference declares protected: sequence C; // The underlying container public: bool empty () const; // whether it is empty size_type size () const; // size reference top (); // take the top element const_reference top () const; void push (const value_type & X); // press the stack void POP (); // output stack };
************** * ******/Template <class t, class sequence> bool operator = (const stack <t, sequence> & X, const stack <t, sequence> & Y) // determine whether two adapters are equal {return X. C = y. c;} template <class T, class sequence> bool operator <(const stack <t, sequence> & X, const stack <t, sequence> & Y) // determine whether adapter X is smaller than adapter y {return X. c <Y. c;} template <class T, class sequence> bool stack <t, sequence >:: empty () const // The const keyword {return C. empty ();} template <class T, class sequence> // you do not need to add a default parameter outside the class. // if it is written as a size_type stack <t, sequence >:: size () const is incorrect, because size_type is defined in the class, and typename should also be added with typename stack <t, sequence >:: size_type stack <t, sequence >:: size () const {return C. size ();} template <class T, class sequence> typename stack <t, sequence >:: reference stack <t, sequence >:: top () // The underlying implementation is to return the last container element {return C. back ();} template <class T, class sequence> typename stack <t, sequence >:: const_reference stack <t, sequence >:: top () const // overload of the top function {return C. pop_back ();} template <class T, class sequence> void stack <t, sequence >:: push (const value_type & X) // The underlying implementation is to add the element {C. push_back (x);} template <class T, class sequence> void stack <t, sequence>: Pop () // The underlying implementation is to bring up the tail element of the basic container {C. pop_back ();}

References:

[1] STL source code analysis Hou Jie

[2] C ++ primer 4th

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.