stack<int> s;queue<int> q;stack/queue<型態>名稱;
| Stack | 功能 |
|---|---|
| push(x) | 放入元素 |
| pop() | 刪除第一個元素 |
| top() | 讀取最上面的元素 |
| empty() | 是否為空 |
| size() | 內有幾個元素 |
| Queue | 功能 |
|---|---|
| push(x) | 放入元素 |
| pop() | 刪除第一個元素 |
| front() | 讀取最前面的元素 |
| empty() | 是否為空 |
| size() | 內有幾個元素 |
s.push(1)
s.push(1)
s.push(2)
s.push(2)
s.push(3)
s.push(3)
s.top()
s.push(3)
s.top()
s.pop()
s.push(3)
s.top()
s.pop()
s.push(3)
s.pop()
s.push(3)
s.pop()
q.push(3)
q.push(1)
q.push(1)
q.push(2)
q.push(2)
q.push(3)
q.push(3)
q.pop()
q.front()
q.push(3)
q.pop()
q.front()
q.push(3)
q.pop()
q.push(3)
q.pop()
q.push(3)
q.pop()
#include <iostream>
#include <stack>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
stack<型態>名字;
//其餘程式碼
}#include <iostream>
#include <queue>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
queue<型態>名字;
//其餘程式碼
}Q.Stack:
題目說明:
請利用迴圈在stack裡按順序放入1~9
並輸出最頂端的三個
測試輸入:
無輸入
測試輸出:
9
8
7
Q.Queue:
題目說明:
請利用迴圈在queue裡按順序放入1~9
並輸出最前方的三個
測試輸入:
無輸入
測試輸出:
1
2
3
#include <iostream>
#include <stack>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
stack <int> s;
for(int i=1;i<10;i++){
s.push(i);
}
for(int i=0;i<3;i++){
cout<<s.top()<<endl;
s.pop();
}
return 0;
}#include <iostream>
#include <queue>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
queue <int> q;
for(int i=1;i<10;i++){
q.push(i);
}
for(int i=0;i<3;i++){
cout<<q.front()<<endl;
q.pop();
}
return 0;
}priority_queue<型態> 名字;
| Priotiry_queue | 功能 |
|---|---|
| push(x) | 放入元素 |
| pop() | 刪除第一個元素 |
| top() | 讀取最上面的元素 |
| empty() | 是否為空 |
| size() | 內有幾個元素 |
pq
3
pq.push(3)
pq
3
pq.push(3)
pq
3
pq.push(2)
2
pq
3
pq.push(2)
2
pq
3
pq.push(2)
2
pq.top()
pq
3
2
pq.top()
pq.pop()
pq
3
2
pq.top()
pq.pop()
pq
3
2
pq.top()
pq.pop()
pq
2
pq.top()
pq.pop()
對於英文等文字也有先後關係
對於英文等文字也有先後關係
對於英文等文字也有先後關係
先看第一個字
對於英文等文字也有先後關係
先看第一個字
再看第二個字
對於英文等文字也有先後關係
先看第一個字
再看第二個字
對於英文等文字也有先後關係
先看第一個字
再看第二個字
對於英文等文字也有先後關係
先看第一個字
再看第二個字
對於英文等文字也有先後關係
先看第一個字
再看第二個字
Q.priority_queue:
題目說明:
輸入未按照大小排列的三個數字
並輸出最小的
測試輸入:
7 3 6
測試輸出:
3
#include <iostream>
#include <queue>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
priority_queue<int> pq;
int in;
for(int i=0;i<3;i++){
cin>>in;
pq.push(in);
}
while(pq.size()!=1){
pq.pop();
}
cout<<pq.top()<<endl;
return 0;
}priority_queue<型態,vector<型態>,greater<型態>> 名字;#include <iostream>
#include <queue>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
priority_queue<int,vector<int>,greater<int>> pqg;
int in;
for(int i=0;i<3;i++){
cin>>in;
pqg.push(in);
}
cout<<pqg.top()<<endl;
return 0;
}