Stack of stacks in STL

zhaozj2021-02-16  53

The stack mode class in the C standard template library provides some ways to simply operate the stack, which provides the following:

Bool Empty () const; check if the stack is empty, if you return to True, you will return false.

Void Pop (); pops up the object located on the top of the stack, and the number of objects in the stack minus one. Do not return any values.

Void Push (const type & _val); Press the value of the Type type _val in the stack, add one of the objects in the stack. Do not return any values.

Size_type size () const; Return the size of the stack, the number of objects in the stack. Where SIZE_TYPE is a unsigned size type.

Value_type & top (); const value_type & top () const; returns an object located on the top of the stack without modifying the object in the stack.

As you can see, the method provided by the stack class in the standard library is limited, and the size of the stack cannot be defined, and the application will cause the application to crash due to the POP operation of the empty stack, which is unsafe, Therefore, when the stack template can, I will be able to hold the maximum number of objects in the specified stack in the constructor. By referring to the Java class library, it is possible to add the stack to the stack, such as removeall (), Elementat (). The method of redefining is as follows:

Bool Push (T i); if the stack is not full, that is, the number of objects in the stack is smaller than the size of the stack, and the object i is pressed into the stack and returns true; if the stack is full, no operation is performed, return false. Where t is the data type of I, the same below.

T POP (); if the stack is non-empty, pop-up the object located on the top of the stack, the number of objects in the stack minus one. Returns the pop-up object.

T peek (); if the stack is non-empty, returns an object located on the top of the stack and does not modify the object in the stack. Its function is the same as the TOP () method of Stack.

Void transovell (); popping out all the objects in the stack, so that the stack is cleared.

Bool isempty (); check whether the stack is empty, if you return to True, you will return false. Its features are the same as the original STACK's EMPTY () method.

T Elementat (INT I); Returns the i-th object in the stack, where the location of this object is calculated from the bottom of the stack.

Unsigned int size (); Return the size of the stack, the number of objects in the stack.

Bool setMaxSize; if the number of objects in the current stack sets the size of the stack, the number of the maximum objects that the stack can be placed, and return TRUE. Otherwise returning false.

Based on the stack method provided above, I use the assert to determine the legitimacy of the current operation. If the POP operation is not performed on the empty stack, if POP operation is made (it is abnormal, it is different from Exception, Abnormal The generation is caused by the boolean expression after ASSERT, is not an error in runtime). The code for the extended stack mode class is as follows:

//********Stack.h**********//*******Code: hiFrog ********

#include #include using namespace std;

Template Class Stack {Private: Stack S; unsigned int mapize; public: stack (unsigned int msize) {maxSize = msize;} BOOL PUSH (T i) {IF (size ()

T peek () {assert (! S.empty ()); return S.top ();

Void RemoveAll () {while (! s.empty ()) s.pop ();

Bool isempty () {return s.empty ();

T Elementat (INT I) {Assert (i> 0 && i <= size ()); int TMP = (int) s.size (); t value = 0; Stack TMPS; // This uses a temporary stack The value of the element to obtain an element to obtain an element of the position is (int K = TMP; K> I; K -) {TMPS.PUSH (S.TOP ()); assert (! S.empty ()); spop ();} assert (! s.empty ()); value = s.top (); for (int J = TMP; J> i; j--) // S. Fused Elements {s.push (Tmps.top ()); assert (! s.empty ()); tmps.pop ();} return value;}

Unsigned int size () {return (unsigned int) s.size ();

Bool setMaxSize (unsigned int msize) {if (msize> size ()) {maxSize = msize; return true;}}}}}

And write a test program:

#include "stack.h" // # include #include using namespace std;

INT main () {double db_Array [] = {1.1, 2.2, 3.3, 4.4, 5.5}; Stack db_stack (10); int i = 0; while (i <5) {db_stack.push (db_Array [i ]); I ;} cout << db_stack.size () << endl; cout << db_stack.ementat (2) << end1; while (! Db_stack.isempty ()) {cout << db_stack.pop () <

The results of the operation are as follows:

52.25.54.43.32.21.1

转载请注明原文地址:https://www.9cbs.com/read-22387.html

New Post(0)