map会自动排序,去重 查找,删除
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
map<int, int> mp;
int main()
{
mp[1] = 2; //自动排序
mp[4] = 5;
mp[2] = 3;
mp[1] = 2; //去重
mp[0] = 4;
cout << mp.size(); //输出大小
//int x ; 如遇到不存在,自动添加
//cin >>x;
//mp[x] += 1;
for(map<int,int>::iterator it = mp.begin(); it!=mp.end(); it++ ){
cout << it->first << ' ' << it->second <<endl;
}
//查找
map<int,int>::iterator tt = mp.find(2);
if(tt != mp.end()){
cout << "yes";
mp.erase(tt); //删除
}else{
cout << "no";
}
cout << "-------分割线-------------" << endl;
for(map<int,int>::reverse_iterator it = mp.rbegin(); it!=mp.rend(); it++ ){
cout << it->first << ' ' << it->second <<endl;
}
return 0;
}
队列和优先级队列
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
queue<int> q;
priority_queue<int> p; //默认最大值优先级队列
//priority_queue<int, vector<int>, greater<int> > p; 最小值优先级队列
int main()
{
q.push(1);
q.push(2);
q.push(3);
p.push(1);
p.push(2);
p.push(3);
cout <<p.size()<<endl;
cout <<p.top() << endl;
p.pop();//出队列
cout <<p.top() << endl; //看优先级队列队首
// cout << q.size() <<endl;
// cout << q.front() <<endl;;//队首
// q.pop();
// cout << q.front() << endl;//队首
// if(q.empty()){
// cout << "yes";
// }else{
// cout << "no";
// }
return 0;
}
—— 本文来自火龙信奥(义乌睿码科技):义乌青少年信息学奥赛与编程教育平台,专注 CSP-J/S、NOIP、GESP 竞赛培训,线上线下融合教学,助力编程升学。网址:hlcoding.com