One, the queue
The data structure of the queue has been mentioned in our handwritten queue. The queue is a simple data structure. First of all, we can know that the queue is a kind of operation with two operations, and we store related relationships . Sometimes, we also need to store related data structures, so we need to open an array unit to store the unit elements of this queue. Then in order to complete this basic entry and exit operation, we can transform this data structure into a double-pointer data structure. We set a pointer at the head node and the tail node respectively to complete this data structure.
For a more in-depth look, you can watch the handwritten queue section
Second, the realization of the stl of the queue
1, the creation of the queue
queue <type> name ;
2, Common pop-up, push-in, head and tail of the queue
push
#include <iostream>
#include <queue>
using namespace std ;
int main ()
{
queue <int> q ;
q.push (100 ) ;
cout << q.front() << endl ;
return 0 ;
}
pop up
q.pop() ;
Return to the head of the queue
q.front()
//队头
q.back() ;
//队尾
3, common operations
queue size
q.size() ;
Three, priority queue
1. Priority queue
The priority queue is first of all a data structure implemented by changing the data structure of the heap. In essence, the priority queue is a data structure with a concept of value. First, we can write the weight by ourselves to complete the queue. Sort. The writing of the entire weight value requires the implementation of a special library functional priority_queue < type> , the priority queue is output according to the entire weight value relationship, and the maximum heap is output by default.
The performance of this priority is to output the highest priority first, output from high to low
2, the creation of priority queue
priority_queue <type , 容器 , 权值> name
- Create a default priority queue (larger values go out first)
priority_queue <type > name ;
- Quickly create an anti-default priority queue (the one with the smaller value goes out first)
priority_queue <int , vector<int> ,greater<int> >
- Create custom sequences
In our definition sequence, the returned true-false relationship should be the opposite of what we expressed, such as A<B, when we judge true and false, we should give him false. In fact, this is because the priority in the
array is relatively large. The following elements are also
how b is created:Functional way
struct structure construction
functional
#include <iostream>
#include <queue>
#include <functional>
using namespace std ;
bool cmp(int a ,int b )
{
return a > b ;
}
int main ()
{
priority_queue <int , vector<int> ,function<bool(int ,int)>> qu(cmp) ;
qu.push (34);
qu.push (345);
qu.push(354);
while ( qu.size() )
{
cout << qu.top() << endl ;
qu.pop() ;
}
return 0 ;
}
struct construction
complex no longer displayed
3. The basic operation of the priority queue
add function, remove function
//压入队头
q.push() ;
//弹出队尾
q.pop() ;
pop-up queue
q.top() ;
4, basic function
Returns the size of the priority queue
cout << q.size() << endl ;
**Simple implementation process**
#include <iostream>
#include <queue>
#include <functional>
#include <vector>
using namespace std ;
bool cmp(pair<int, int > a, pair<int, int > b)
{
if( a.first != b.first)
return a.first < b.first ;
return a.second > b.second ;
}
int main ()
{
int n ;
int j = 1 ;
cin >> n ;
//输出最大的元素 ,当有多个的时候输出下标最小的
priority_queue <pair<int,int> ,vector<pair<int,int> > ,function<bool(pair<int,int> ,pair<int,int>)> >q(cmp);
for(j = 1 ; j <= n ; j ++ )
{
int x ;
cin >> x ;
q.push({
x , j});
}
for(int i = 1 ; i <= n ; i ++ )
{
auto t = q.top() ;
cout << t.second << endl ;
q.pop() ;
}
return 0 ;
}