C++ - STL - queue

C ++ - STL - queue organize your notes

A basic concept:

  queue is a FIFO data structure, there are two outlets, one end of the new element, the other end of the removal element;

Queue only team head and the tail can be used outside, and therefore the queue does not allow traversal behavior .

  c ++ template class is defined in the queue <queue> header file, queue template class template requires two parameters, one is the element type, a container type, the element type is required, the container type is optional, defaults to deque type.

Second header

  #include<queue>

Three define a queue class queue

  queue <type> name;

Four correlation function

( . 1) Push () element was added at the end of a

 

#include <iostream>
#include <queue>
using namespace std;
int main ()
{
	queue<int> q;
	q.push(1);
	q.push(2);
	cout<<q.front()<<endl;
	return 0;
}

  Output: 1

( 2) Front () returns a reference to the first element in the queue. Note: only the first element of the return value, and not deleted!

 

#include <iostream>
#include <queue>
using namespace std;
int main ()
{
	queue<int> q;
	q.push(1);
	q.push(2);
	cout<<q.front()<<endl;
	return 0;
}

  Output: 1

( . 3) POP () function to delete a queue element

 

#include <iostream>
#include <queue>
using namespace std;
int main ()
{
	queue<int> q;
	q.push(1);
	q.push(2);
	q.pop();
	cout<<q.front()<<endl;
	return 0;
}

  Output: 2

( . 4) empty () function, if the queue is empty, returns true (1), if the queue is not empty, otherwise flase (0).

 

#include <iostream>
#include <queue>
using namespace std;
int main ()
{
	queue<int> q;
	q.push(18);
	cout << "first queue (unique) element:" << q.front () << endl;
	cout << "At this time, the return value is" << q.empty () << endl;
	q.pop();
	cout << "after excluding elements, a first queue value (no):" << q.front () << endl; 
	cout << "At this time, the return value is" << q.empty () << endl;
	return 0;
}

 

  

 

  Output:

  A first queue (unique) element: 18
  In this case the return value is 0
  removed after the element, the first queue value (no): 0
  In this case the return value is 1

( . 5) Back () returns a reference to the last element of the queue. Note: only the return value of the last element, not deleted!

 

#include <iostream>
#include <queue>
using namespace std;
int main ()
{
	queue<int> q;
	q.push(1);
	q.push(2);
	cout<<q.back()<<endl;
	return 0;
}

  Output: 2

( . 6) size () returns the number of elements in the queue

#include <iostream>
#include <queue>
using namespace std;
int main ()
{
	queue<int> q;
	q.push(1);
	cout<<q.size()<<endl;
	return 0;
}

  Output: 1

Guess you like

Origin www.cnblogs.com/yxbl/p/12275353.html