0
点赞
收藏
分享

微信扫一扫

C++ stl汇总

何晓杰Dev 2022-06-17 阅读 35

vector
​​​ https://blog.csdn.net/weixin_42172261/article/details/86604772​​

map
​​​ https://blog.csdn.net/weixin_42172261/article/details/86621807​​

set
​​​ https://blog.csdn.net/weixin_42172261/article/details/86607669​​

迭代器
​​​ https://blog.csdn.net/weixin_42172261/article/details/86606300​​

stack
​​​ https://blog.csdn.net/weixin_42172261/article/details/88924120​​

queue
​​​ https://blog.csdn.net/weixin_42172261/article/details/88924295​​

priority_queue

#include <iostream>
#include <queue>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main(){
//从大到小排列
priority_queue<int, vector<int>, less<int> > q;
srand((unsigned)time(NULL));
for (int i=1; i<=5; i++){
int t = rand() % 20;
q.push(t);
}
while (!q.empty()){
int t = q.top();
q.pop();
cout<<t<<endl;
}
return 0;
}
//从小到大排列
priority_queue<int, vector<int>, greater<int> > q;
#include <iostream>
#include <queue>
#include <ctime>
#include <cstdlib>
using namespace std;
//重载小于号,a和b越大的优先级越低,越排在后面
struct Node{
int a, b;
bool operator<(const Node& t) const{
if (a!=t.a) return a>t.a;
else return b>t.b;
}
};

int main(){
priority_queue<Node> q;
srand((unsigned)time(NULL));
for (int i=1; i<=5; i++){
Node t;
t.a = rand() % 20;
t.b = rand() % 20;
q.push(t);
}
while (!q.empty()){
Node t = q.top();
q.pop();
cout<<t.a<<" "<<t.b<<endl;
}
return 0;
}
//重载小于号,a和b越小的优先级越低,越排在后面 
struct Node{
int a, b;
bool operator<(const Node& t) const{
if (a!=t.a) return a<t.a;
else return b<t.b;
}
};


举报

相关推荐

C++ STL 算法汇总

C++(STL)

C++ (STL)

【C++】STL容器

C++(七)——STL

C++《STL全集》

0 条评论