当前位置:网站首页>STL notes (10): container adapter - queue and stack
STL notes (10): container adapter - queue and stack
2022-07-21 14:10:00 【Reyn Morales】
STL note ( Ten ): Container adapter —— Queues and stacks
function
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
using namespace std;
// Sequence type Relevance Container adapter bitset
// vector set stack
// deque map queue
// list priority_queue
// Container adapter : On the basis of sequential containers, it is encapsulated as a stack 、 Queue or priority queue
// stack - First in, then out - No iterator
// Basic container for encapsulation :deque( Default )、vector、list
// Basic operation of encapsulation :empty、size、top、push、pop
void test1()
{
stack<int> s1; // deque
vector<int> v(2); // 0 0
stack<int, vector<int> > s2(v); // vector
cout << s2.empty() << endl;
cout << s2.size() << endl;
cout << s2.top() << endl; // 0
s2.push(3);
s2.push(4);
cout << s2.top() << endl; // 4
s2.pop();
cout << s2.top() << endl; // 3
// Traverse out of stack
while (!s2.empty()) {
cout << s2.top() << ' ';
s2.pop();
}
}
// queue - fifo - No iterator
// Basic container for encapsulation :deque( Default )、list
// Basic operation of encapsulation :empty、size、front、back、push、pop
void test2()
{
queue<int> q1;
list<int> l;
l.push_back(3);
l.push_back(4);
queue<int, list<int> > q2(l);
cout << q2.empty() << endl; // 0
cout << q2.size() << endl; // 2
cout << q2.front() << endl; // 3
cout << q2.back() << endl; // 4
q2.push(5);
q2.push(6);
q2.push(7);
cout << q2.size() << endl; // 5
cout << q2.front() << endl; // 3
cout << q2.back() << endl; // 7
q2.pop();
q2.pop();
cout << q2.size() << endl; // 3
cout << q2.front() << endl; // 5
cout << q2.back() << endl; // 7
// Traverse the queue
while(!q2.empty()) {
cout << q2.front() << ' ';
q2.pop();
}
}
int main()
{
// Container adapter
test1(); // Stack
test2(); // queue
return 0;
}
Case study
#include <iostream>
#include <string>
#include <stack>
#include <queue>
using namespace std;
// Parentheses matching
class Match
{
private:
string bracket;
public:
Match(string s): bracket(s) {}
void matchBracket()
{
stack<char> stk;
int i = 0, flag = 0; // matching · 0 | Mismatch · 1
for (; i < bracket.size(); i++) {
char c = bracket[i];
switch(c)
{
case '[':
stk.push(c);
break;
case '(':
stk.push(c);
break;
case ')':
if (!stk.empty() && stk.top() == '(') {
stk.pop();
} else {
flag = 1;
}
break;
case ']':
if (!stk.empty() && stk.top() == '[') {
stk.pop();
} else {
flag = 1;
}
break;
}
if (flag == 1) break;
}
if (flag == 0 && stk.empty()) { // In extreme cases : Only left bracket or only right bracket
cout << " The match is successful !" << endl;
} else { // Only the left parenthesis , Only the right parenthesis , Bracket mismatch
cout << " Matching failure !" << endl;
}
}
};
class YHTriangle
{
private:
int rowNum;
public:
YHTriangle(int n): rowNum(n) {}
void printYHTriangle()
{
int s, t;
queue<int> q;
cout << '1' << endl; // first line
q.push(0);
q.push(1);
q.push(1); // The second line
for (int i = 0; i < rowNum - 2; i++) { // Print the second line and generate the third line
q.push(0); // 0 Represents the boundary value of each row ( Beginning of each line )| Zero is always one , Used to generate a... At the beginning and end of each line
do
{
s=q.front();
q.pop();
t=q.front();
q.push(s+t);
if(t) {
cout<<t<<"\t";
} else {
cout<<endl; // Line break when reading zero
}
}while(t != 0);
}
q.pop(); // 0
while(!q.empty()){ // Print the last line
cout<<q.front()<<"\t";
q.pop();
}
}
};
int main()
{
Match r("[()]");
Match s("[(");
Match t("[(])");
r.matchBracket();
s.matchBracket();
t.matchBracket();
YHTriangle tmp(6);
tmp.printYHTriangle();
return 0;
}
summary
- Both stack and queue are container adapters , That is, functional packaging based on sequential containers , The container and function of the package depend on the characteristics of the container adapter itself
- According to the characteristics of the container adapter itself , There are certain restrictions on the sequential containers encapsulated at the bottom , for example , For queues , Its bottom layer can only be used deque And list, They can't be used vector, Because the queue is a data structure that can be operated at the head and tail of the queue , and vector The operation of can only be carried out at the end of the container
In the next article , We will introduce C++ Container adapter in —— Priority queue
Postscript
This article is mainly used as a summary of the course learning process , If you have any questions, you can also leave a message in the comment area , I will try my best to help you solve the problem , If it's helpful to you , I hope you like it more 、 forward , thank you !
边栏推荐
猜你喜欢
随机推荐
DQL where查询
【软件测试】测试中的风险有哪些?
Is it safe and reliable to open an account on the flush?
技术人如何打响个人品牌?五大顶级KOL独家传授
_ Sort query
Safe day 2022 China large scale agile conference will be held on November 5
Is it safe to open an account in flush? How much commission can you give?
IO input / output stream
MySQL---three 多表查询与事务的操作
Win11找不到gpedit.msc怎么办?Win11无法打开gpedit.msc解决教程
The White House is trying to promote the filling of hundreds of thousands of cyber security jobs in the United States
How to test wechat applet
Easygbs platform setup tips: how to hide the platform web page from being accessed?
(22) blender source code analysis: mouse down message to window call process
Win11 Excel文件变成白板图标怎么解决?
电脑突然显示只有C盘,其他磁盘不显示了----解决方法(很简单)
Hisilicon AI chip (hi35xx): image JPG to BGR upgrade
助力企业数字化升级,火山引擎发布云上增长解决方案
[program compilation (preprocessing operation) + link]
5款经典代码阅读器的使用方案对比