STL数据结构
STL数据结构
1.priority_queue
#include<queue>
pritority<int>q;(大根堆)
priority_queue<int,vector<int>,greater<int> >q;(小根堆)
struct
no{
int
x,v;
bool
operator <(
const
no &T)
const
{
return
v>T.v;}
// v值xiao的优先
};
queue<no>q;
2.vector
#include<vector>
vector<int>vec;
vec.push_back();(加入,从0开始)
vec.size()
vec.pop_back() (删除末尾)
vec.clear();
使用迭代器访问元素.
vector<int>::iterator it;
for(it=vec.begin();it!=vec.end();it++)
cout<<*it<<endl;
插入元素: vec.insert(vec.begin()+i,a);在第i+1个元素前面插入a;
删除元素: vec.erase(vec.begin()+2);删除第3个元素
vec.erase(vec.begin()+i,vec.end()+j);删除区间[i,j-1];区间从0开始
排序 sort(vec.begin(),vec.end(),cmp)
元素翻转 reverse(vec.begin(),vec.end());
3.map
#include<map>
map<int,int>
直接用
4.bitset
#include<bitset>
bitset<100> s;
相当于flag[100]
也可以当做100位的二进制数
效率O(n/32)
5.set
include<set>
set<int>S//去重
multiset<int>S//不去重
S.begin()第一个
S.end()最后一个的下一位(没有东西)
S.clear()
S.empty() ,判断set容器是否为空
S.count(x);x出现次数
erase(iterator) ,删除定位器iterator指向的值
erase(key_value),删除键值key_value的值
S.size()
lower_bound(key_value) ,返回第一个大于等于key_value的定位器
upper_bound(key_value),返回第一个大于key_value的定位器
插入 S.inset(x);
迭代器 set<int
>::iterator pos;
遍历 for(pos=S.begin();pos!=S.end();pos++)
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;
int main(){
set<int>S;
S.clear();
S.insert(1);S.insert(2);S.insert(3);
set<int>::iterator it;
it=S.begin();
cout<<*it<<' '<<*S.begin()<<endl;
//查找
set<int>::iterator t1,t2;
t1=S.lower_bound(1);t2=S.upper_bound(1);
cout<<*t1<<' '<<*t2<<endl;
set<int>::iterator t3;
t3=S.upper_bound(2);
cout<<"? "<<*t3<<endl;
//找不到就指向end
//遍历
for(it=S.begin();it!=S.end();++it){
cout<<*it<<endl;
}
//统计/ 判断是否出现
cout<<S.count(1)<<endl;
//删除
it=S.end();
S.erase(it);
cout<<*S.end()<<endl;
return 0;
}
6.next_permutation(a+1,a+n+1)
7.nth_element(first,kth,last,cmp)
nth_element(now+1,now+m+1,now+n+1,greater<ll>());
注意前m个是无序的,但保证是前m大
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构