STL入门
2026-07-17 20:51:10
发布于:广东
动态数组
push_back(x) 插入元素 扩容
pop_back() 删除最后一个元素
size() 获得长度
empty() 判断是否为空
front() 第一个元素
back() 最后一个元素
clear() 清空
erase(a.begin() + pos)
时间复杂度 O(n)
删除第 pos 下标的元素
长度 -1
insert(a.begin() + pos, x)
时间复杂度 O(n)
在第 pos 下标位置插入元素 x
长度 +1
begin() 第一个下标
end() 最后一个下标的下一个位置
sort(a.begin(),a.end()) 动态数组排序
sort(a.begin(),a.end(),cmp)
栈
stack<int> a;
1.push(x) 入栈
2.pop() 出栈 注意:必须要有元素
3.top() 获得栈顶元素
4.size() 获得栈长度
5.empty() 栈是否为空

括号匹配

队列
stack<int> s;
vector<int> a;
queue<int> q;
1.q.push(x); 队尾加入元素
2.q.front(); 获得队头元素
3.q.pop(); 弹出队头元素
4.q.size() 获得队列长度
5.q.empty() 判断是否为空
6.q.back() 获得队尾元素
while(q.size()){
cout<<q.front();
q.pop();
}
银行取号 1
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
queue<string> nq;
queue<string> vq;
string now = "";
while(n--){
string s;
cin >> s;
if(s == "end"){
if(!vq.empty()){
now = vq.front();
vq.pop();
}else if(!nq.empty()){
now = nq.front();
nq.pop();
}else{
now = "";
}
}else{
if(now == ""){
now = s;
}else{
if(s[0] == 'V'){
vq.push(s);
}else{
nq.push(s);
}
}
}
}
while(!vq.empty()){
cout << vq.front() << " ";
vq.pop();
}
while(!nq.empty()){
cout << nq.front() << " ";
nq.pop();
}
return 0;
}
选做题
银行取号系统

银行取号1

银行取号2

排队

冠军

过山车

约瑟夫环
#include<bits/stdc++.h>
using namespace std;
int main(){
long long n,m;
cin >> n >> m;
if(m == 1){
for(int i = 1;i <= n;i++)cout << i << ' ';
return 0;
}
vector<int> a;
for(int i = 1;i <= n;i++){
a.push_back(i);
}
long long pos = 0; // pos 报数同学
while(!a.empty()){
pos = (pos + m - 1) % a.size();
cout << a[pos] << ' ';
a.erase(a.begin() + pos);// 删除动态数组第pos个位置
}
return 0;
}
全部评论 6
1
4天前 来自 广东
1不错不错
4天前 来自 广东
11
4天前 来自 广东
01
4天前 来自 广东
01
4天前 来自 广东
01
4天前 来自 广东
0





























有帮助,赞一个