Files
python/C++专题课程/STL基础知识/STL-2-栈和队列.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

48 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>
using namespace std;
/*
知识点内容:C++ STL 栈和队列详解
栈是一种特殊的线性表。其特殊性在于限定插入和删除数据元素的操作只能在线性表的一端进行。
队列(Queue)也是一种运算受限的线性表,它的运算限制与栈不同,是两头都有限制,插入只能在表的一端进行(只进不出),
而删除只能在表的另一端进行(只出不进),允许删除的一端称为队尾(rear),允许插入的一端称为队头 (Front),
文档内容参考:
https://www.cnblogs.com/aiguona/p/7200837.html
* */
int main()
{
//队列
queue<int> q;
//栈
stack<char> s;
//放入第一个元素1
q.push(1);
//判断是不是为空
cout << q.empty() << endl; //如果栈为空返回true,否则返回false
//放入第二个元素2
q.push(2);
cout << q.front() << endl;
q.pop();
// 返回队首元素的值,但不删除该元素
cout << q.front() << endl;
//删除队列首元素但不返回其值
q.pop();
//如果栈为空返回true,否则返回false
cout << q.empty() <<endl;
//======================================================================
//存入元素2
s.push(2);
cout << s.top() <<endl;
//返回栈顶的元素,但不删除该元素
s.push(3);
cout << s.top();
s.pop();
cout << s.top();
}