Introduction to C ++ container usage-stack container adapter and stack container

Source: Internet
Author: User

Introduction to C ++ container usage-stack container adapter and stack container

I. Introduction

Stack is a container adapter (STL containers are divided into sequential containers and associated containers. Container adapters are more binding containers for packaging these two types of containers ), it is designed to be used in scenarios where operations are performed first (FILO) structures. In this case, the insertion and deletion of elements can only be performed at the end of the container.

Stack is implemented through container adapters. It is a class that uses a specific container class as its underlying container. It provides some specific member functions to access its own elements, the elements can only follow the specific container, that is, the top of the stack, to perform the operations of the stack and the inbound stack.

The bottom-layer container can be any standard container template class, or some container classes with specific purposes. They should support the following operations:

Empty (judge whether it is empty) size (return the number of elements in the stack) back (return the top element of the stack) push_back (into the stack) pop_back (out of the stack) standard container class, such as vector, deque and list to meet the preceding requirements. If the container to be used is not explicitly specified, deque is used by default.

Ii. Function usage example

1. Structure and Analysis (C ++ 11)

View plain copy

Initialize (1) explicitstack (constcontainer_type & ctnr );

Move-initialize (2) explicitstack (container_type & ctnr = container_type ());

Allocator (3) templateexplicitstack (constAlloc & alloc );

Init + allocator (4) templatestack (constcontainer_type & ctnr, constAlloc & alloc );

Move-init + allocator (5) templatestack (container_type & ctnr, constAlloc & alloc );

Copy + allocator (6) templatestack (conststack & x, constAlloc & alloc );

Move + allocator (7) templatestack (stack & x, constAlloc & alloc );

(1) initialize the constructor

Construct a container adapter whose internal elements are copies of ctnr

(2) move-initialization constructor

Construct a container adapter whose internal elements are obtained by moving the ctnr Value

I have not understood the remaining few with splitters.

Cplusplus example:

View plain copy

// Constructingstacks # include // std: cout

# Include // std: stack # include // std: vector

# Include // std: deque

Intmain (){

Std: dequemydeque (3,100); // dequewith3elementsstd: vectormyvector (2,200); // vectorwith2elements

Std: stackfirst; // emptystack

Std: stacksecond (mydeque); // stackinitializedtocopyofdeque

Std: stack> third; // emptystackusingvectorstd: stack> fourth (myvector );

Std: cout

2. empty ()

Returns whether the current stack is empty (when its size is 0). The empty () function cannot empty the stack, but returns a const function of the bool type.

View plain copy

Stacks; s. push (1 );

S. push (2); cout <

3. size ()

Returns the number of elements in the container, time complexity O (1). the return value type is size_type, that is, unsigned int. The size () function cannot change the stack size.

For example, the above Code segment should output 2

4. top ()

Returns the reference of the top element of the stack.

Because the stack is an advanced post-release structure, the top element is the last element inserted into the stack.

(In c ++ 11, a reference or const_reference is automatically returned Based on the element type. Calling the top function for an empty stack will terminate abnormally. Therefore, use the empty () function to check in advance)

View plain copy

Stacks; s. push (1 );

S. push (2); cout

5. push () and emplace () (C ++ 11)

Both the push () function and emplace () function Insert a new element at the top of the container stack.

Push () is actually the push_back () function of the underlying container called. The value of the new element is a copy of the push function parameter.

Emplace () is actually the emplace_back () function of the underlying container called. The value of the new element is constructed locally within the container and does not need to be moved or copied.

Stack emplace can also be used in common basic types.

View plain copy

StructNode {

Node (intx): x (x ){}

Intx ;};

Intmain ()

{Stacks1;

S1.emplace (1); s1.push (Node (2 ));

Stacks2;

S2.emplace (1); // OKreturn0;

}

6. pop ()

Delete the top element to reduce the stack size.

The deleted element is the element just inserted into the stack. This element is consistent with the return value of the top function. This function calls the object's destructor (if any). pop () is actually implemented using the pop_back () function of the underlying container.

(Pop () for an empty stack will lead to abnormal program termination. empty should be used for early check)

View plain copy

Stacks; s. push (1 );

S. push (2); s. push (3 );

Cout

The stack does not have clear or erase functions. to clear a stack, you need to call the stack function cyclically.

View plain copy

Stacks; // s. erase (); // error

// S. clear (); // errors. push (1 );

S. push (2); s. push (3 );

Cout

7. swap ()

Exchange the content of two stacks (all elements). This function exchanges the underlying container by using the non-member function swap (). time complexity O (1)

Cplusplus example

View plain copy

// Stack: swap # include // std: cout

# Include // std: stack

Intmain (){

Std: stackfoo, bar; foo. push (10); foo. push (20); foo. push (30 );

Bar. push (111); bar. push (222 );

Foo. swap (bar );

Std: cout

}

8. Operator Overloading

View plaincopy

(1) = is used to compare whether two stacks are equal. First, judge whether the size is equal, and then use operator = to determine whether the elements are equal. It will stop at the first unequal place.

Templatebooloperator = (conststack & lhs, conststack & rhs );

(2 )! = And = are the opposite. As long as the size is different or one element is different, it is not equal.

Template

Booloperator! = (Conststack & lhs, conststack & rhs );

(3) starting from the first element, compare to stop when the result is found to be greater than or equal to. If one stack is the prefix of the other stack, the long stack is large. Same below

Template

Booloperator & lhs, conststack & rhs );

(4)

Template

Booloperator <= (conststack & lhs, conststack & rhs );

(5)

Template

Booloperator> (conststack & lhs, conststack & rhs );

(6)

Template

Booloperator >=( conststack & lhs, conststack & rhs );

View plaincopy

Stackl1; l1.emplace (1 );

L1.emplace (2); l1.emplace (3 );

Stackl2; l2.emplace (1 );

L2.emplace (2); cout

Iv. Conclusion

Stack is a commonly used data structure that can be seen in various programming languages or operating systems. Common DFS algorithms, recursive algorithms, context switches, process scheduling, and registers.

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.