stl:queue 源码
In C++ STL, Queue is a type of container that follows FIFO (First-in-First-Out) elements arrangement i.e. the elements which insert first will be removed first. In queue, elements are inserted at one end known as "back" and are deleted from another end known as "front".
在C ++ STL中,队列是遵循FIFO(先进先出)元素排列的一种容器,即,首先插入的元素将被首先删除。 在队列中,元素被插入称为“ back”的一端,并从称为“ front”的另一端删除。
1)C ++ STL queue :: front()函数 (1) C++ STL queue::front() function)
The function front() returns the reference to the first element in the queue i.e. the oldest element in the queue, so it is used to get the first element from the front of the list of a queue.
函数front()返回对队列中第一个元素(即队列中最旧的元素)的引用,因此它用于从队列列表的开头获取第一个元素。
Syntax:
句法:
queue_name.front();
2)C ++ STL queue :: back()函数 (2) C++ STL queue::back() function)
The function back() returns the reference to the last element in the queue i.e. the newest element in the queue, so it is used to get the first element from the back of the list of a queue.
函数back()返回对队列中最后一个元素(即队列中的最新元素)的引用,因此它用于从队列列表的后面获取第一个元素。
Syntax:
句法:
queue_name.back();
Program:
程序:
// cpp program for queue implementation
// Example of front() and back()
#include <iostream>
#include <queue>
using namespace std;
//Main function
int main()
{
// declaring an empty queue
queue<int> Q;
//inserting elements
Q.push(10);
Q.push(20);
Q.push(30);
Q.push(40);
Q.push(50);
cout<<"First element of the queue: "<<Q.front()<<endl;
cout<<"Last element of the queue : "<<Q.back()<<endl;
//removing two elements
Q.pop();
Q.pop();
cout<<"After removing two elements..."<<endl;
cout<<"First element of the queue: "<<Q.front()<<endl;
cout<<"Last element of the queue : "<<Q.back()<<endl;
return 0;
}
Output
输出量
First element of the queue: 10
Last element of the queue : 50
After removing two elements...
First element of the queue: 30
Last element of the queue : 50
翻译自: https://www.includehelp.com/stl/queue-front-and-queue-back.aspx
stl:queue 源码