Learning C ++: adaptive container (stacks and queues)

Introduction
Standard Template Library (STL) provides a number of containers (such as stack, queue, priority_queue), use these containers to simulate the behavior of stacks and queues.
Such a container for internal use, but presents another container vessel known as adaptive behavior characteristics.

1. The behavior characteristics of the stacks and queues

Stacks and queues with an array or list is very similar, but to insert, and delete elements of access methods are certain restrictions. Elements can be inserted into any position and can remove the element determines the behavior characteristics of container from any position.
Stack : Stack is a last-out system, only insert or remove elements from the top of the stack.
Queue : queues are FIFO system, the element is inserted into the tail removed from the queue head, the first element is inserted into the first deleted. Queue can be considered in the cinema ticket queue of people, to join the queue of people to leave.

2. Use the STL stack class

Generic STL container std :: stack simulate this behavior stack. To use the stack, you must include the header file:

#include <stack>

STL stack class is a template. Allow the element at the top of insert and delete elements, but does not allow access in the middle.
In implementations STL, std :: stack is defined as follows:
Template <elementType class, class Container = the deque> class stack;
parameter elementType stack is stored in the object type
second template parameter Container container bottom stack using the default implementation class. The default is to use std :: deque to store data, but the vector or list to store the data can specify.
Examples of the stack
creating a storage element of the double stack type:
stack stackDoubles;
stack creates a storage class (e.g., Tuna) objects:
stack stackTunas;
create double type element is stored, and the vector used as the underlying container stacks:
stack :: < double, vector> stackDoublesInVector;
using a copy of the object stack to create another stack object:
stack stackIntsCopy (stackInts);
member function stack of
stack change another container (e.g. deque, list or vector) acts, through the restriction element inserting or deleting way to achieve its functionality to provide behavioral characteristics strict compliance with the stack mechanism.
1.push
stackInts.push (25); // insert elements in the stack
2.pop
stackInts.pop (); // remove elements of the stack
3.empty
IF (stackInts.empty ()) {the DoSomething;} // determines whether the stack is empty
4.size
size_t nNum stackInts.size = (); // Returns the number of elements in the stack
5.top
stackInts.top (); // get a pointer to reference the top element

3. STL queue class

STL queue is a template class. Only allowed at the end of the insertion element and removes elements from the beginning; queue does not allow access to the middle of the element, but you can access the elements of the beginning and end.
Generic STL container std :: queue to simulate the behavior of this queue. To use the queue, you must include the header file:

#include <queue>

std :: queue is defined as follows:
Template <class elementType, the deque class Container => class queue;
first parameter elementType, the type of elements contained in the object queue.
The second parameter Container, which is the set of types of data for storage. The default is deque, may also be provided as a list or vector.
Examples of the queue
to create a storage type int queues:
queue qIntegers;
create a storage element type as a double, storing and using these elements std :: list:
queue <double, List> qDoublesInList;
use of another example of a queue queue:
queue qCopy (qIntegers);
member function queue of
1.push
qIntegers.push (25); // insert an element in the tail
2.pop
qIntegers.pop (); // delete the head of the queue elements
3.front
qIntegers.front ( ); // returns a pointer to refer to the first team of
4.back
qIntegers.back (); // returns a pointer to the tail of the reference
5.empty
iF (qIntegers.empty ()) {} // check if the queue is empty
6.size
// returns the number of elements in the queue; size_t nNum = qIntegers.size ()

4. STL priority queue priority_queue

STL priority_queue is a template class, which must also include the header file:

#include <queue>

priority_queue differs with the queue that contains the maximum value of the elements located in the first team (by default), and can perform operations in the first team.
Examples of the
defined class std :: priority_queue follows:
Template <elementType is class, class Container = Vector, class Compare = less> The priority_queue class
third parameter specifies a binary predicate Compare. Default std :: less, in descending order, that is the maximum value for the first team. Greater use of small to large.
Examples of an integer priority queue:
The priority_queue pqIntegers;
create an element type as a double, and in ascending order of the priority queue storage std :: deque in:
The priority_queue <int, the deque, Greater> pqIntegers_Inverse;
using another example of a priority_queue to priority_queue:
pqIntegers pqCopy (pqIntegers);
priority_queue member function
1.push
pqIntegers.push (10); // element is inserted in a priority queue
2.pop
pqIntegers.pop (); // delete team first element, namely the largest element
3.top
pqIntegers.top (); // returns the first element of the queue squadron references (ie the largest element)
4.empty
IF (pqIntegers.empty ()) {} // Check whether the air
5.size
pqIntegers.size (); // returns the number of elements in the priority queue

Published 12 original articles · won praise 13 · views 619

Guess you like

Origin blog.csdn.net/wjinjie/article/details/104273674
Recommended