往期回顾:
C++ 入门15:STL 容器之列表(list)_c++列表-优快云博客
C++ 入门18:STL 容器之栈(stack)与队列(queue)
1. 什么是栈(stack)
栈(Stack)是一种遵循先进后出(LIFO,Last In First Out)原则的线性数据结构。在栈中,元素的插入和删除操作都发生在栈的顶部。栈的常见应用包括函数调用栈、表达式求值等。
基本特性
- 插入和删除操作都发生在栈顶。
- 后插入的元素先被删除。
栈的基本操作
引入头文件
在使用栈之前,需要引入头文件 <stack>
。
#include <stack>
创建栈
我们可以通过默认构造函数创建一个空栈,也可以使用初始化列表创建栈。
示例代码:
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> intStack; // 创建一个空栈
// 向栈中压入元素
intStack.push(10);
intStack.push(20);
intStack.push(30);
return 0;
}
常用成员函数
|
|
|
|
|
示例代码:
stack<int> intStack;
intStack.push(10);
intStack.push(20);
intStack.push(30);
cout << "Top of the stack: " << intStack.top() << endl; // 输出 30
intStack.pop(); // 弹出栈顶元素
cout << "Top of the stack after pop: " << intStack.top() << endl; // 输出 20
cout << "Stack size: " << intStack.size() << endl; // 输出 2
if (intStack.empty()) {
cout << "The stack is empty!" << endl;
} else {
cout << "The stack is not empty!" << endl;
}
栈的应用
栈常用于处理递归问题、表达式的求值等。例如,括号匹配问题、逆波兰表达式的计算等,都可以用栈来解决。
2. 什么是队列(queue)
队列(Queue)是一种遵循先进先出(FIFO,First In First Out)原则的线性数据结构。在队列中,元素的插入发生在队尾,删除发生在队头。队列的常见应用包括任务调度、打印队列等。
基本特性
|
|
|
队列的基本操作
引入头文件
在使用队列之前,需要引入头文件 <queue>
。
#include <queue>
创建队列
我们可以通过默认构造函数创建一个空队列,也可以使用初始化列表创建队列。
示例代码:
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> intQueue; // 创建一个空队列
// 向队列中压入元素
intQueue.push(10);
intQueue.push(20);
intQueue.push(30);
return 0;
}
常用成员函数
|
|
|
|
|
|
示例代码:
queue<int> intQueue;
intQueue.push(10);
intQueue.push(20);
intQueue.push(30);
cout << "Front of the queue: " << intQueue.front() << endl; // 输出 10
cout << "Back of the queue: " << intQueue.back() << endl; // 输出 30
intQueue.pop(); // 弹出队头元素
cout << "Front of the queue after pop: " << intQueue.front() << endl; // 输出 20
cout << "Queue size: " << intQueue.size() << endl; // 输出 2
if (intQueue.empty()) {
cout << "The queue is empty!" << endl;
} else {
cout << "The queue is not empty!" << endl;
}
队列的应用
队列常用于排队问题、任务调度等场景。例如,操作系统中的进程调度通常使用队列,消息队列用于线程间通信等。
3. 栈(stack)与队列(queue)的区别
特性 | 栈(stack) | 队列(queue) |
数据结构类型 | LIFO(后进先出) | FIFO(先进先出) |
元素插入 | 在栈顶插入元素 | 在队尾插入元素 |
元素删除 | 在栈顶删除元素 | 在队头删除元素 |
常见应用 | 表达式求值、括号匹配、递归等 | 任务调度、进程调度、消息队列等 |
4. 栈与队列的高级应用
栈的高级应用:括号匹配
栈常用于括号匹配问题,例如判断一个字符串中的括号是否正确配对。
示例代码:
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isValidParentheses(const string& s) {
stack<char> stack;
for (char c : s) {
if (c == '(') {
stack.push(c);
} else if (c == ')') {
if (stack.empty()) {
return false;
}
stack.pop();
}
}
return stack.empty();
}
int main() {
string s = "(()())";
if (isValidParentheses(s)) {
cout << "Valid parentheses" << endl;
} else {
cout << "Invalid parentheses" << endl;
}
return 0;
}
队列的高级应用:任务调度
队列常用于任务调度,例如操作系统中的进程调度通常使用队列来实现。
示例代码:
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<string> taskQueue;
// 模拟任务入队
taskQueue.push("Task 1");
taskQueue.push("Task 2");
taskQueue.push("Task 3");
// 处理任务
while (!taskQueue.empty()) {
cout << "Processing " << taskQueue.front() << endl;
taskQueue.pop(); // 任务出队
}
return 0;
}
结语
以上就是 C++ 标准模板库中的栈和队列的基础知识点了。栈遵循先进后出的原则,而队列遵循先进先出的原则。掌握栈和队列的使用,基本上在开发中都会用到的,大家多看看,一定要掌握。
都看到这里了,点个赞再走呗朋友~
加油吧,预祝大家变得更强!