#创作计划#stl容器(未完成)勿喷
2025-08-31 15:31:40
发布于:上海
点赞过50,主播更新下一期(我不配当主播,不要取关我!!!!!!)
😍
@AC君
点个赞呗(你们不给,会伤害一个小朋友的心的)
1.set:集合
1.没有重复的元素{1,1,2,3,4,4} -> {1,2,3,4}
2.自动排序
s.insert(x) x = 3,4,1 {1,3,4}
打印:迭代器
for(set<int>::iterator it = s.begin(); it != s.end();it++)
#include<bits/stdc++.h> using namespace std; int main(){ set<int> s; s.insert(4); s.insert(3); s.insert(1); for(ser<int>::iterator it = s.begin();it != s.end();it++){//等于for(auto it = s.begin();it != s.end();it++) cout << *it << endl; }//查找数字存在,返回迭代器 //不存在,返回end() auto it = s.find(5); if(s.find(5) == s.end()){ 不存在 }else{ 存在 } return 0; } //输出: //1 //3 //4
2.栈
首先呢,头文件十分重要:
//死记硬背 #include <stack> //我:其实#include <bits/stdc++.h>也可以 //老师:你万能头玩的真好 //我:我默写头文件就默写这个 //老师:*****。
然后,就是栈是什么了:
我的回答:栈就是一种容器,后进先出,就像一把枪,先进的子弹,最后发射
老师的回答:栈就是一种STL容器,STL是什么,后面再说。在笔记本上写好,栈的特点就是后进先出,抬头,看这幅图...
AI的回答:栈是一种特殊的线性表,它只能在表的一端(称为栈顶)进行插入和删除操作。这就像一个只能从顶部取放物品的容器,最后放入的物品会最先被取出,因此栈具有 “后进先出”(Last In First Out,LIFO)的特点。
看不懂没事:
请看VCR:
(这东西只有我能画出来了)
这下来,就是代码了:
我来说,就那么几个part(部分):
1.定义:
stack<int>st; //固定名称 + 数组变量类型 + DIY栈名称
2.上图第一个步骤:入栈
本意就是将一个元素进入这个容器栈
int x; cin >> x; st.push(x); //将x(输入来的元素)压进栈
3.出栈
st.pop();//将最上面的元素弹出
4.输出栈顶元素(最上面的元素):
cout << st.top() << " ";
5.判断栈是否为空:
cout << st.empty(); //如果栈为空时,输出1,否则,输出0;
6.栈de大小
cout << st.size(); //输出栈的大小;
理清知识点,下一步就是做题了:
老师:大家看一下这题题目,用其他方法的给我抄十遍代码。。。
我:残暴的老师于是我给出了以下代码:
/* #include<iostream> using namespace std; int st[105]; int top=0; void push(int x){ top++; st[top]=x; } void pop(){ top--; } int TOP(){ return st[top]; } int size(){ return top; } bool empty(){ return top==0; } int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ string zl; cin>>zl; if(zl=="push"){ int x; cin>>x; push(x); } else if(zl=="pop"){ if(empty()) cout<<"pop fail"<<endl; else{ cout<<"pop "<< TOP()<<endl; pop(); } } else if(zl=="top"){ if(empty()) cout<<"top fail"<<endl; else cout<<"top = "<<TOP()<<endl; } else if(zl=="size") cout<<"size = "<<size()<<endl; else if(zl=="empty"){ if(empty()) cout<<"yes"<<endl; else cout<<"no"<<endl; } } return 0; } */ #include <iostream> #include <stack> using namespace std; int main() { int n, x; cin >> n; string c; stack <int> s; for(int i = 1; i <= n; ++i) { cin >> c; if(c == "push") { cin >> x; s.push(x); } else if(c == "pop") { if(!s.empty()) { cout << "pop " << s.top() << endl; s.pop(); } else { cout << "pop fail" << endl; } } else if(c == "top") { if(!s.empty()) { cout << "top = " << s.top() << endl; } else { cout << "top fail" << endl; } } else if(c == "size") { cout << "size = " << s.size() << endl; } else if(c == "empty") { if(!s.empty()) { cout << "no" << endl; } else { cout << "yes" << endl; } } } return 0; } //老师:这么写是吧! //我:老师罚不了我 //老师:这么喜欢写代码,给我抄俩遍 //我:为我花生!!!
3. 队列(queue)
类似于一根管子,先进先出。
入队
void push(int x){ q[++rear]=x; }
出队
void pop(){ front++; }
队首
int query(){ return q[front+1]; }
大小
int size(){ return rear-front; }
判空
bool empty(p){ return rear==front; }//以上代码中,rear是队尾,front是队首前一个元素
队列模板 定义:queue<int> q; 入队:q.push(x); 出队:q.pop(); 队首:int t=q.front(); 大小:q.size(); 判空:q.empty(); 初始化/清空:while(q.empty())q.pop();
其中,只有初始化复杂度是O(n),其余均为O(1)。
优先队列(priority queue)
定义:priority_queue<int> q(大根堆) priority_queue<int,vector<int>,greater<int> > q(小根堆) 其余和队列基本相同,但是q.pop();是删掉最大元素,没有front(),只有q.top(),代表最大的元素。
注: q.top()的复杂度是O(log n)。
主播vector打不动了
请看 VCR!!!!!!!!
list
他的发明将不会打链表的你投向了天堂(他插入,删除很方便(o(1)))
请看主播手写的图片(好像不是图片)
#include<bits/stdc++.h> using namespace std; int main(){ list<int> li; li.push_back(2); li.push_back(1); li.push_back(4); for(auto it = li.begin();it != li.end();it++){//list<int>::iterator 的万能替代品 -> auto cout << *it << endl; }auto k = find(li.begin(),li.end(),1); li.erase(k); cout << endl; for(auto it = li.begin();it != li.end();it++){ cout << *it << endl; } cout << endl; int a[6] = {1,1,3,4,5}; cout << unique(a,a + 5) - a;//在a数组中去重然后输出a数组中的元素数量 return 0; } /* list:链表 1.链表易于插入和删除,不易于查找 list<int> li; 2.插入 li.push_front(x);//头插 li.push_back(x); //尾插 3.遍历:迭代器 4.删除 用find()先找到数据的地址(<algorithm>中自带的函数,需包含三个参数,起点,终点,访问元素) li.erase(k); */
求求各位大佬点个赞吧
欢迎补充
全部评论 1
事实上你不能否定有牛逼的xxs的存在
4天前 来自 浙江
0(谁说xxs不能爆切STL的
4天前 来自 浙江
0明白了
2天前 来自 上海
0
有帮助,赞一个