I. INTRODUCTION
In the implementation of algorithms and data structures, many places we need to queue (follow FIFO, first-out principle).
In order to use the queue, we can use the array to implement the queue, but it is too cumbersome to write, and it is also very error-prone.
Fortunately, the STL (Standard Template Library) of C + + implements a powerful queue for us, which is included in the header file <queue>.
Two. Queue
A) constructor
Here's an example to show the queue's constructor
deque<int> Deck (3, -); List<int> MyList (2, -); Queue<int> First;//Default ConstructsQueue<int,list<int> > second (mylist);//construct a list as a containerQueue<int> third (mylist);//constructed with deque as a container, where deque is the default container for the queueQueue<int,deque<int> > Forth (mylist);//constructed with default container, Deque is the default container for queue
We can use the Deque (double-ended queue container) or list (linked list container) as the base container for the queue (underlying container, that is, the queue is implemented based on the underlying container), where deque is used by default, if not specifically specified in the parameters, The queue then uses deque as the base container.
b) Other member functions
- Empty to test whether the container is empty and returns True when NULL
- Size returns the sizes of the containers
- Front returns the first element of a queue, the element that was first pressed into the queue
- Back returns the last element of the queue, the element that was pressed into the queue at the latest
- Push adds elements to the end of the queue
- Pop popup Queue first element
- Swap (c++11) swap two queues
- Emplace (C++11) constructs elements directly in the container, you can refer to the C++11 new feature emplace operation
Three. Priority_queue (priority queue)
A) constructor
1#include <cstdio>2#include <queue>3#include <cstdlib>4#include <iostream>5#include <ctime>6#include <functional>7 using namespacestd;8 structnode{9 intA;TenNode (inta): A (a) {} One }; A structmycomparision{ - BOOLreverse; -Mycomparision (Const BOOL&revparam=false){ theReverse =Revparam; - } - BOOL operator() (Const int& LHS,Const int&RHS)Const { - if(reverse)return(LHS > RHS);//Ascending + Else return(LHS < RHS);//Descending - } + }; A structcmp{ at BOOL operator() (ConstNode A,ConstNode b)Const{ - returnA.A < B.A;//less than symbol for descending output - } - }; - intMain () { - intMyints[] = {Ten, -, -, -}; inpriority_queue<int>Zero; -Priority_queue<node,vector<node>,cmp> first;//custom structure's priority queue, descending output to for(intc:myints) { + First.push (Node (c)); - } thepriority_queue<int,vector<int>,mycomparision> Second (myints,myints+4, Mycomparision (true));//Custom sort function with custom functor Mycomparision *priority_queue<int,vector<int>,mycomparision> Third (myints,myints+4, Mycomparision ()); $priority_queue<int,vector<int>,mycomparision> Forth (myints,myints+4);//Output with thirdPanax Notoginsengpriority_queue<int,vector<int>,less<int>> Fifth (myints,myints+4);//as a result, the queue first outputs decimals with third,less, which is the default, that is, less<int> can omit -priority_queue<int,vector<int>,greater<int>> Sixth (myints,myints+4);//use the functor greater in the functional library to give the queue priority output decimals the while(!First.empty ()) { +cout << First.top (). A <<" "; A First.pop (); the } +cout <<Endl; - while(!Second.empty ()) { $cout << second.top () <<" "; $ Second.pop (); - } -cout <<Endl; the while(!Third.empty ()) { -cout << third.top () <<" ";Wuyi Third.pop (); the } -cout <<Endl; Wu while(!Forth.empty ()) { -cout << forth.top () <<" "; About Forth.pop (); $ } -cout <<Endl; - while(!Fifth.empty ()) { -cout << fifth.top () <<" "; A Fifth.pop (); + } thecout <<Endl; - while(!Sixth.empty ()) { $cout << sixth.top () <<" "; the Sixth.pop (); the } the return 0; the}
The heap is maintained inside the priority queue. Use the heap to achieve random
We can use the Deque (double-ended queue container) or vector (vector container) as the base container for priority_queue, where vectors are used by default, and if not specifically specified in the parameters, the queue uses vectors as the underlying container.
It is also important to pay special attention to the use of parody functions. In header file <functional> provides a subset of functor functions that we can use to implement ascending descending operations on basic data types. But for a custom struct type, we need to do it ourselves extra to implement the functor to sort. For the concept of functor, refer to the blog: "C + + STL" in-depth analysis of mysterious---functor
b) Other member functions
The member functions of the priority_queue are broadly consistent with the queue, which should be noted:
- Top replaces the original front, each time taking the element with the most value in a particular collation
- Cancel the back function
The above is my own information summary of some of the use of the queue, if there is a wrong to look at you treatise.
STL queue for C + + learning notes