火龙信奥
  • 首页
  • 课程
  • 题库
  • 打卡
    • 代码对战
    • 快速对战
  • 题单
  • 团队
  • 荣誉墙
  • 商城
  • 登录 / 注册

暑假算法-map

作者: 作者的头像   huolong , 时间:2022-07-05 11:40:49 , 所有人可见, 阅读  5

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

©2026加盟我们 | 关于我们 | ACM课程 | 常见问题 | 成果墙 | 评测记录 | 浙ICP备2021013995号
在线画图 | OI WIki | 打字练习
火龙信奥
请输入登录信息


请完成安全验证
验证码底图 滑块
向右拖动滑块完成验证
请输入用户名 / 绑定的手机号码



请输入注册信息(手机号验证码注册)





验证码5分钟有效,60秒内不可重复获取,每日最多3次

微信登录

微信登录二维码

正在生成二维码...

账号已过期,请续期。
去续期

绑定手机号

📱

为了更好地保护您的账号安全,享受完整的平台服务

请您尽快绑定手机号码