網站首頁 編程語言 正文
Stack的介紹和使用
stack的文檔介紹
stack是一種容器適配器,專門用在具有后進先出操作的上下文環境中,其刪除只能從容器的一端進行元素的插入與提取操作。 stack是作為容器適配器被實現的,容器適配器即是對特定類封裝作為其底層的容器,并提供一組特定的成員函數來訪問其元素,將特定類作為其底層的,元素特定容器的尾部(即棧頂)被壓入和彈出。
stack的默認定義的模板
注意:
默認情況下stack是以deque作為底層容器。
(大多數情況下都是使用queue作為底層容器即可,我們需要變動的只是存儲類型)
方式一:規定的存儲類型
//int類型
stack<int> st1;
//double類型
stack<double> st2;
方式二:規定底層實現容器
//用list
stack<int, list<int>> st1;
//用vector
stack<int, vector<int>> st2;
stack的使用
函數說明 | 接口說明 |
empty() | 檢測stack是否為空 |
size() | 返回stack中元素的個數 |
top() | 返回棧頂元素的引用 |
push() | 將元素val壓入stack中 |
pop() | 將stack中尾部的元素彈出 |
#include<iostream>
#include<stack>
using namespace std;
int main() {
stack<int> st;
for (int i = 0; i < 10; i++) {
st.push(i);
}
//0 1 2 3 4 5 6 7 8 9
cout << st.size() << endl;//輸出:10
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
cout << endl; //輸出:9 8 7 6 5 4 3 2 1 0
return 0;
}
queue的介紹和使用
queue的文檔介紹
隊列是一種容器適配器,專門用于在FIFO上下文(先進先出)中操作,其中從容器一端插入元素,另一端提取元素。 隊列作為容器適配器實現,容器適配器即將特定容器類封裝作為其底層容器類,queue提供一組特定的成員函數來訪問其元素。元素從隊尾入隊列,從隊頭出隊列。
queue的默認定義的模板
注意:
默認情況下queue是以deque作為底層容器。
(大多數情況下都是使用queue作為底層容器即可,我們需要變動的只是存儲類型)
方式一:規定的存儲類型
//int類型
queue<int> st1;
//double類型
queue<double> st2;
方式二:規定底層實現容器
//用list
stack<int, list<int>> st1;
//用vector
stack<int, vector<int>> st2;
queue的使用
函數聲明 | 接口說明 |
empty() | 檢測隊列是否為空,是返回true,否則返回false |
size() | 返回隊列中有效元素的個數 |
front() | 返回隊頭元素的引用 |
back() | 返回隊尾元素的引用 |
push() | 在隊尾將元素val入隊列 |
pop() | 將隊頭元素出隊列 |
#include <iostream>
#include <queue>
using namespace std;
int main(){
queue<int> q;
for (int i = 0; i < 10; i++) {
q.push(i);
}
//0 1 2 3 4 5 6 7 8 9
cout << q.size() << endl; //輸出:10
while (!q.empty()){
cout << q.front() << " ";
q.pop();
}
cout << endl; //輸出:0 1 2 3 4 5 6 7 8 9
return 0;
}
原文鏈接:https://blog.csdn.net/weixin_64609308/article/details/127638775
相關推薦
- 2022-03-24 android?Launcher?AppWidget添加步驟介紹_Android
- 2022-11-28 一文帶你搞懂Golang結構體內存布局_Golang
- 2022-07-18 Nio中Buffer的Scattering和Gathering
- 2024-01-12 Springboot測試類中 @Before與@BeforeEach的區別
- 2023-04-02 pytorch中關于distributedsampler函數的使用_python
- 2022-12-10 Flutter改變狀態變量是否必須寫在setState回調詳解_Android
- 2022-06-16 golang?gorm開發架構及寫插件示例_Golang
- 2021-12-11 Redis之sql緩存的具體使用_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支