C++,stl,map/multimap详解

目录

1.map的构造和赋值 

2.map的大小和交换

3.map的插入和删除

4.map的查找和统计

5.map的排序


1.map的构造和赋值 

#include<bits/stdc++.h>
using namespace std;void print(map<int,int> &mp)
{for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it++){cout << it -> first << ' ' << it -> second;cout << endl;}cout << endl;
}int main()
{map<int,int> mp;mp.insert(pair<int,int>(1,90));mp.insert(pair<int,int>(2,950));mp.insert(pair<int,int>(7,920));mp.insert(pair<int,int>(5,10));mp.insert(pair<int,int>(6,80));//按照key的值进行排序print(mp);return 0;
}

 

2.map的大小和交换

#include<bits/stdc++.h>
using namespace std;void print(map<int,int> &mp)
{for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it++){cout << it -> first << ' ' << it -> second;cout << endl;}cout << endl;
}int main()
{map<int,int> mp;mp.insert(pair<int,int>(1,90));mp.insert(pair<int,int>(2,950));mp.insert(pair<int,int>(7,920));mp.insert(pair<int,int>(5,10));mp.insert(pair<int,int>(6,80));//按照key的值进行排序print(mp);cout << mp.empty() << endl;cout << mp.size() << endl;map<int,int>mp1;mp1.swap(mp);print(mp1);return 0;
}

3.map的插入和删除

#include<bits/stdc++.h>
using namespace std;void print(map<int,int> &mp)
{for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it++){cout << it -> first << ' ' << it -> second;cout << endl;}cout << endl;
}int main()
{map<int,int> mp;mp.insert(pair<int,int>(1,90));mp.insert(pair<int,int>(2,950));mp.insert(make_pair(7,920));mp.insert(pair<int,int>(5,10));mp[6] = 80;//按照key的值进行排序print(mp);mp.erase(6);//按照key删除mp.erase(mp.begin());//也可以传迭代器进行删除print(mp);mp.clear();print(mp);return 0;
}

4.map的查找和统计

#include<bits/stdc++.h>
using namespace std;void print(map<int,int> &mp)
{for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it++){cout << it -> first << ' ' << it -> second;cout << endl;}cout << endl;
}int main()
{map<int,int> mp;mp.insert(pair<int,int>(1,90));mp.insert(pair<int,int>(2,950));mp.insert(make_pair(7,920));mp.insert(pair<int,int>(5,10));mp[6] = 80;map<int,int>::iterator it = mp.find(5);//从前往后找map里有没有key为5的,没找到的话迭代器就在mp.end()处if(it != mp.end()) cout << "找到了" << endl;cout << mp.count(7) << endl;//输出有几个key为7的数,这里只有一个return 0;
}

5.map的排序

改成从大到小 

#include<bits/stdc++.h>
using namespace std;class cmp
{
public:bool operator()(int v1,int v2){return v1 > v2;}
};int main()
{map<int,int,cmp> mp;mp.insert(pair<int,int>(1,90));mp.insert(pair<int,int>(2,950));mp.insert(make_pair(7,920));mp.insert(pair<int,int>(5,10));mp[6] = 80;for(map<int,int,cmp>::iterator it = mp.begin(); it != mp.end(); it++){cout << it -> first << ' ' << it -> second << endl;}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/681308.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据库管理-第150期 Oracle Vector DB AI-02(20240212)

数据库管理150期 2024-02-12 数据库管理-第150期 Oracle Vector DB & AI-02&#xff08;20240212&#xff09;1 LLM2 LLM面临的挑战3 RAG4 向量数据库LLM总结 数据库管理-第150期 Oracle Vector DB & AI-02&#xff08;20240212&#xff09; 作者&#xff1a;胖头鱼的鱼…

2.8:Maefile、计算单词个数、判断文件类型、单词逆置

1.有main.c&#xff0c;test.c&#xff0c;test1.c&#xff0c;创建Makefile 程序代码&#xff1a; Makefile: 1 CCgcc2 EXEhello3 OBJS$(patsubst %.c,%.o,$(wildcard *.c))4 CFLAGS-c -o5 all:$(EXE)6 7 #hello依赖test.o main.o8 $(EXE):$(OBJS)9 $(CC) $^ -o $10 …

除了ajax还有什么方法获取数据而不用刷新数据

除了AJAX&#xff08;Asynchronous JavaScript and XML&#xff09;&#xff0c;还有以下几种方法可以在不刷新页面的情况下获取数据&#xff1a; Fetch API&#xff1a;Fetch API 是一个现代的网络 API&#xff0c;提供了一个 JavaScript Promise 对象来处理网络请求。Fetch …

腾讯云4核8G服务器多少钱?646元一年零3个月

腾讯云服务器4核8G配置优惠价格表&#xff0c;轻量应用服务器和CVM云服务器均有活动&#xff0c;云服务器CVM标准型S5实例4核8G配置价格15个月1437.3元&#xff0c;5年6490.44元&#xff0c;轻量应用服务器4核8G12M带宽一年446元、529元15个月&#xff0c;腾讯云百科txybk.com分…

算法学习——LeetCode力扣二叉树篇8

算法学习——LeetCode力扣二叉树篇8 669. 修剪二叉搜索树 669. 修剪二叉搜索树 - 力扣&#xff08;LeetCode&#xff09; 描述 给你二叉搜索树的根节点 root &#xff0c;同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树&#xff0c;使得所有节点的值在[low, high…

[leetcode]买卖股票的最佳时机 (动态规划)

121. 买卖股票的最佳时机 给定一个数组 prices &#xff0c;它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。 你只能选择 某一天 买入这只股票&#xff0c;并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。 返回你可以从…

恒创科技:香港 BGP 服务器网络连通性如何测试?

随着互联网的快速发展&#xff0c;网络连通性测试变得越来越重要。网络连通性测试的目的是确定网络设备之间的连接是否正常&#xff0c;以及数据包是否能够在网络中顺利传输。本文将介绍一种简单易行的香港 BGP 服务器网络连通性的测试方法&#xff0c;利用tracer测试工具。这里…

springboot184基于springboot的校园网上店铺的设计与实现

简介 【毕设源码推荐 javaweb 项目】基于springbootvue 的 适用于计算机类毕业设计&#xff0c;课程设计参考与学习用途。仅供学习参考&#xff0c; 不得用于商业或者非法用途&#xff0c;否则&#xff0c;一切后果请用户自负。 看运行截图看 第五章 第四章 获取资料方式 **项…

幻兽帕鲁 Linux 服务器迁移完成之后,进入游戏会出现闪退?怎么解决?

主要的原因是迁移的存档文件&#xff0c;新服务器可能没有操作存档文件的权限&#xff0c;不能成功更新存档&#xff0c;从而导致闪退。 建议&#xff1a;在 Linux 服务器内&#xff0c;依次运行如下命令后&#xff0c;再次尝试连接游戏&#xff1a; 第一步&#xff1a; s…

分布式认证JWT

JWT解释 JWT是一种加密后的数据载体&#xff0c;可在各应用间进行数据传输。 JWT的组成 包含3部分。header&#xff08;头&#xff09;、payload&#xff08;有效载荷&#xff09;、signature&#xff08;签名&#xff09;。格式是header.payload.signature Header组成 JWT…

JAVA-数组乱序

实现步骤 假设有一组数组numbers从数组中最后一个元素开始遍历设置一个随机数作为循环中遍历到的元素之前的所有元素的下标&#xff0c;即可从该元素之前的所有元素中随机取出一个每次将随机取出的元素与遍历到的元素交换&#xff0c;即可完成乱序 实例如下&#xff1a; im…

day39 Bootstrap——容器简括

前言 前言Bootstrap5 容器容器内边距容器的边框和颜色响应式容器 前言 Bootstrap&#xff0c;来自 Twitter&#xff0c;是目前最受欢迎的前端框架。Bootstrap 是基于 HTML、CSS、JAVASCRIPT 的&#xff0c;它简洁灵活&#xff0c;使得 Web 开发更加快捷。 Bootstrap5 容器 B…

2022年美赛A题个人思路

一、读题 自行车运动员获胜的机会不同&#xff0c;但取决于赛事的类型、路线和自行车运动员的能力。 能量曲线&#xff1a;表明自行车运动员可以产生给定能量的时间长短对于特定的时间长度&#xff0c;能量曲线提供了自行车运动员在给定时间内保持的最大能量。 &#xff08;1&a…

wayland(xdg_wm_base) + egl + opengles 纹理贴图进阶实例(四)

文章目录 前言一、使用gstreamer 获取 pattern 图片二、代码实例1. pattern 图片作为纹理数据源的代码实例1.1 基于opengles2.0 接口的 egl_wayland_texture2_1.c1.2 基于opengles3.0 接口的 egl_wayland_texture3_1.c2. xdg-shell-client-protocol.h 和 xdg-shell-protocol.c3…

大模型LoRA知识

什么是 LoRA&#xff1f; LoRA&#xff08;low-rank adaptation of large language models&#xff09;是一种针对大型语言模型进行低秩适应的技术。大型语言模型通常具有数十亿个参数&#xff0c;这使得它们在计算和存储方面非常昂贵。低秩适应的目标是通过将语言模型的参数矩…

Gateway微服务网关

Spring Cloud Gateway Spring Cloud Gateway 是 Spring Cloud生态系统中的网关&#xff0c;它是基于Spring 5.0、SpringBoot 2.0和Project Reactor等技术开发的&#xff0c;旨在为微服务架构提供一种简单有效的、统一的API路由管理方式&#xff0c;并为微服务架构提供安全、监…

【力扣】5.最长回文子串

这道题我主要是通过动态规划来进行解题&#xff0c;看了我好久&#xff08;解析&#xff09;&#xff0c;生疏了呀。 首先就是判断一个字符串是不是回文&#xff0c;我们可以设置两个指针&#xff0c;从前往后进行判断即可&#xff0c;运用暴力解题法&#xff0c;这里运用的动…

【生产实测可用】Redis修改集群弱口令

起因 漏扫redis连接发现弱口令需要修改 先连上去看看是空口令还是弱口令 redis-cli -p 6379 -h a.b.c.d info sentinel找到启动服务器的配置文件 cp -av /app/redis-7001/redis.conf /app/redis-7001/redis.conf.bak20240207 echo "requirepass 口令" >>/a…

Gitee的使用教程(简单详细)

1.安装git&#xff08;我的电脑自带git&#xff0c;我没弄这步QAQ&#xff09; Git (git-scm.com)https://git-scm.com/ 安装好后在桌面点击鼠标右键会出现git GUI 和 git Bash&#xff08;没有的话点击显示更多选项&#xff09; 2.去gitee上注册一个账号 工作台 - Gitee.co…

Qt杂记——TCP

1. if(m_tcpSocket!nullptr){m_tcpSocket->flush();m_tcpSocket->abort();m_tcpSocket->deleteLater();m_tcpSocket nullptr;} &#xff08;1&#xff09;m_tcpSocket->flush() 调用m_tcpSocket->flush()函数可以强制将发送缓冲区中的数据立即发送出去&#…