队列限定尾端插入和首端删除。

- #include
- using namespace std;
-
- queue<int> q;
| 函数名 | 作用 |
|---|---|
| back() | 返回最后一个元素 |
| empty() | 如果队列空则返回真 |
| front() | 返回第一个元素 |
| pop() | 删除第一个元素 |
| push() | 在末尾加入一个元素 |
| size() | 返回队列中元素的个数 |
- #include <iostream>
- #include <queue>
- using namespace std;
- int main() {
- queue<int> q;//空对象
- q.push(2);//尾部插入
- q.push(3);
- q.push(1);
- q.push(4);
- cout<<q.size()<<endl;//元素个数
- if (!q.empty()) { //若true,则队列中无元素
- int temp = q.front(); //获取队头元素
- cout << temp << endl;
- temp = q.back(); //获取队尾元素
- cout << temp << endl;
- q.pop();//弹出队头元素
- }
- return 0;
- }
感谢现在的好奇,为了能成为更好的自己。