c++ STL map简介

2023-11-04

首先头文件,

#include<map>

反正我用

#include<bits/stdc++.h>

然后就是创建一个map:

map<string,int>mymap;//类似于一个字典的映射关系,string和int可以改成其他的类型
mymap是你定义的map的名字,可以自己改
string可以改为pair<int,int>来表示两个数之间存在关系

再就是添加元素啦:

//第一种方法是用类似于数组的方法添加:
mymap[0]=hihihi;
//第二种是用insert插入:
mymap.insert(pair<int,string>(1,"hahaha"));
//其它的方法:http://t.zoukankan.com/wuchunming-p-3780677.html

map排序:

//先定义一个结构体来规定排序方式
struct rule{
	bool operator()(int a,int b){
		return a>b;
	}
};


//再将此排序方式加入到map定义中:
map<char,int,rule>a;

然后就是输出啦:

	for(auto it=mymap.begin();it!=mymap.end();it++)
	cout<<it->first<<" "<<it->second;

 map呢有一个自动根据key值排序的功能,也就是it->first,这个很好用哦

看例题:

问题 E: 8.4.4 硬木种类-2

题目描述

某国有数百种硬木树种,该国自然资源部利用卫星成像技术编制了一份特定日期每棵树的物种清单。计算每个物种张所有种群的百分比。

输入

输入包括每棵树的物种清单,每行一棵树。物种名称不超过30个字符,不超过10000种,不超过1000000棵树。

输出

按字母顺序输出植物种群中代表的每个物种的名称,然后是占所有种群的百分比,保留小数点后4位。

样例输入 复制

Red Alder Ash Aspen Basswood Ash Beech Yellow Birch Ash Cherry Cottonwood Ash Cypress Red Elm Gum Hackberry White Oak Hickory Pecan Hard Maple White Oak Soft Maple Red Oak Red Oak White Oak Poplan Sassafras Sycamore Black Walnut Willow

样例输出 复制

Ash 13.7931 Aspen 3.4483 Basswood 3.4483 Beech 3.4483 Black Walnut 3.4483 Cherry 3.4483 Cottonwood 3.4483 Cypress 3.4483 Gum 3.4483 Hackberry 3.4483 Hard Maple 3.4483 Hickory 3.4483 Pecan 3.4483 Poplan 3.4483 Red Alder 3.4483 Red Elm 3.4483 Red Oak 6.8966 Sassafras 3.4483 Soft Maple 3.4483 Sycamore 3.4483 White Oak 10.3448 Willow 3.4483 Yellow Birch 3.4483

map直接秒杀!

#include<bits/stdc++.h>
using namespace std;
int main(){
    map<string,int>mymap;
    string temp;
    int num=0;
    while(getline(cin,temp))
    {
        mymap[temp]++;
        num++;
         
    }
    for(auto it=mymap.begin();it!=mymap.end();it++)
    {
        cout<<it->first<<" ";
        cout<<fixed<<setprecision(4)<<it->second*1.0/num*100<<endl;
    }
}

例题:

C - FF

Problem Statement

Takahashi runs an SNS "Twidai," which has

N users from user 1 through user

N. In Twidai, users can follow or unfollow other users.

Q operations have been performed since Twidai was launched. The i-th (1≤i≤Q) operation is represented by three integers Ti​, Ai​, and

Bi​, whose meanings are as follows:

  • If

Ti​=1: it means that user Ai​ follows user Bi​. If user Ai​ is already following user

  • Bi​ at the time of this operation, it does not make any change.
  • If
  • Ti​=2: it means that user Ai​ unfollows user Bi​. If user Ai​ is not following user
  • Bi​ at the time of this operation, it does not make any change.
  • If
  • Ti​=3: it means that you are asked to determine if users Ai​ and Bi​ are following each other. You need to print Yes if user Ai​ is following user Bi​ and user Bi​ is following user
    • Ai​, and No otherwise.

    When the service was launched, no users were following any users.

    Print the correct answers for all operations such that

    Ti​=3 in ascending order of

    i.

    Constraints

  • 2≤N≤109
  • 1≤Q≤2×105
  • Ti​=1,2,3 (1≤i≤Q)
  • 1≤Ai​≤N (1≤i≤Q)
  • 1≤Bi​≤N (1≤i≤Q)
  • Ai​=Bi​ (1≤i≤Q)
  • There exists
  • i (1≤i≤Q) such that
    • Ti​=3.
    • All values in the input are integers.

    Input

    The input is given from Standard Input in the following format:

     
    N 
    Q
    
    T1​ 
    A1​ 
    B1​
    
    T2​ 
    A2​ 
    B2​
    
    TQ​ 
    AQ​ 
    BQ​
    

    Output

    Print

    X lines, where X is the number of i's (1≤i≤Q) such that Ti​=3. The j-th (1≤j≤X) line should contain the answer to the j-th operation such that

    Ti​=3.


    Sample Input 1

    Copy

    3 9
    1 1 2
    3 1 2
    1 2 1
    3 1 2
    1 2 3
    1 3 2
    3 1 3
    2 1 2
    3 1 2
    

    Sample Output 1

    Copy

    No
    Yes
    No
    No
    

    Twidai has three users. The nine operations are as follows.

    • User
    1 follows user
  • 2. No other users are following or followed by any users.
  • Determine if users
  • 1 and 2 are following each other. User 1 is following user 2, but user 2 is not following user
  • 1, so No is the correct answer to this operation.
  • User
  • 2 follows user
  • 1.
  • Determine if users
  • 1 and 2 are following each other. User 1 is following user 2, and user 2 is following user
  • 1, so Yes is the correct answer to this operation.
  • User
  • 2 follows user
  • 3.
  • User
  • 3 follows user
  • 2.
  • Determine if users
  • 1 and 3 are following each other. User 1 is not following user 3, and user 3 is not following user
  • 1, so No is the correct answer to this operation.
  • User
  • 1 unfollows user
  • 2.
  • Determine if users
  • 1 and 2 are following each other. User 2 is following user 1, but user 1 is not following user
    • 2, so No is the correct answer to this operation.

    Sample Input 2

    Copy

    2 8
    1 1 2
    1 2 1
    3 1 2
    1 1 2
    1 1 2
    1 1 2
    2 1 2
    3 1 2
    

    Sample Output 2

    Copy

    Yes
    No
    

    A user may follow the same user multiple times.


    Sample Input 3

    Copy

    10 30
    3 1 6
    3 5 4
    1 6 1
    3 1 7
    3 8 4
    1 1 6
    2 4 3
    1 6 5
    1 5 6
    1 1 8
    1 8 1
    2 3 10
    1 7 6
    3 5 6
    1 6 7
    3 6 7
    1 9 5
    3 8 6
    3 3 8
    2 6 9
    1 7 1
    3 10 8
    2 9 2
    1 10 9
    2 6 10
    2 6 8
    3 1 6
    3 1 8
    2 8 5
    1 9 10
    

    Sample Output 3

    Copy

    No
    No
    No
    No
    Yes
    Yes
    No
    No
    No
    Yes
    Yes
    

     map直接秒杀:
     

    #include<bits/stdc++.h>
    using namespace std;
    
    
    int main()
    {
    	int n,q;
    	cin>>n>>q;
    	map<pair<int,int>,bool>mp;
    	while(q--)
    	{
    		int flag=0;
    		cin>>flag;
    		if(flag==1)
    		{
    			int a,b;
    			cin>>a>>b;
    			mp[{a,b}]=1;
    		}
    		if(flag==2)
    		{
    			int a,b;
    			cin>>a>>b;
    			mp[{a,b}]=0;
    		}
    		if(flag==3)
    		{
    			int a,b;
    			cin>>a>>b;
    			if(mp[{a,b}]&&mp[{b,a}])
    			cout<<"Yes"<<endl;
    			else
    			cout<<"No"<<endl;
    		}
    	}
    }

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c++ STL map简介 的相关文章

  • 如何在MVVM中管理多个窗口

    我知道有几个与此类似的问题 但我还没有找到明确的答案 我正在尝试深入研究 MVVM 并尽可能保持纯粹 但不确定如何在坚持模式的同时启动 关闭窗口 我最初的想法是向 ViewModel 发送数据绑定命令 触发代码来启动一个新视图 然后通过 X
  • 如何检查图像对象与资源中的图像对象是否相同?

    所以我试图创建一个简单的程序 只需在单击图片框中更改图片即可 我目前只使用两张图片 所以我的图片框单击事件函数的代码 看起来像这样 private void pictureBox1 Click object sender EventArgs
  • 如何使 Windows 窗体的关闭按钮不关闭窗体但使其不可见?

    该表单有一个 NotifyIcon 对象 当用户单击 关闭 按钮时 我希望表单不关闭而是变得不可见 然后 如果用户想再次查看该表单 可以双击系统托盘中的图标 如果用户想关闭表单 可以右键单击该图标并选择 关闭 有人可以告诉我如何使关闭按钮不
  • 获取按下的按钮的返回值

    我有一个在特定事件中弹出的表单 它从数组中提取按钮并将标签值设置为特定值 因此 如果您要按下或单击此按钮 该函数应返回标签值 我怎样才能做到这一点 我如何知道点击了哪个按钮 此时代码返回 DialogResult 但我想从函数返回 Tag
  • 从父类调用子类方法

    a doStuff 方法是否可以在不编辑 A 类的情况下打印 B did stuff 如果是这样 我该怎么做 class Program static void Main string args A a new A B b new B a
  • 未解决的包含:“cocos2d.h” - Cocos2dx

    当我在 Eclipse 中导入 cocos2dx android 项目时 我的头文件上收到此警告 Unresolved inclusion cocos2d h 为什么是这样 它实际上困扰着我 该项目可以正确编译并运行 但我希望这种情况消失
  • 为什么#pragma optimize("", off)

    我正在审查一个 C MFC 项目 在某些文件的开头有这样一行 pragma optimize off 我知道这会关闭所有以下功能的优化 但这样做的动机通常是什么 我专门使用它来在一组特定代码中获得更好的调试信息 并在优化的情况下编译应用程序
  • 在 Visual Studio 2008 上设置预调试事件

    我想在 Visual Studio 中开始调试程序之前运行一个任务 我每次调试程序时都需要运行此任务 因此构建后事件还不够好 我查看了设置的 调试 选项卡 但没有这样的选项 有什么办法可以做到这一点吗 你唯一可以尝试的 IMO 就是尝试Co
  • C#:如何防止主窗体过早显示

    在我的 main 方法中 我像往常一样启动主窗体 Application EnableVisualStyles Application SetCompatibleTextRenderingDefault false Application
  • Cython 和类的构造函数

    我对 Cython 使用默认构造函数有疑问 我的 C 类 Node 如下 Node h class Node public Node std cerr lt lt calling no arg constructor lt lt std e
  • WPF TabControl,用C#代码更改TabItem的背景颜色

    嗨 我认为这是一个初学者的问题 我搜索了所有相关问题 但所有这些都由 xaml 回答 但是 我需要的是后台代码 我有一个 TabControl 我需要设置其项目的背景颜色 我需要在选择 取消选择和悬停时为项目设置不同的颜色 非常感谢你的帮助
  • 指针减法混乱

    当我们从另一个指针中减去一个指针时 差值不等于它们相距多少字节 而是等于它们相距多少个整数 如果指向整数 为什么这样 这个想法是你指向内存块 06 07 08 09 10 11 mem 18 24 17 53 7 14 data 如果你有i
  • 在 ASP.NET Core 3.1 中使用包含“System.Web.HttpContext”的旧项目

    我们有一些用 Net Framework编写的遗留项目 应该由由ASP NET Core3 1编写的API项目使用 问题是这些遗留项目正在使用 System Web HttpContext 您知道它不再存在于 net core 中 现在我们
  • 如何衡量两个字符串之间的相似度? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 给定两个字符串text1 and text2 public SOMEUSABLERETURNTYPE Compare string t
  • Discord.net 无法在 Linux 上运行

    我正在尝试让在 Linux VPS 上运行的 Discord net 中编码的不和谐机器人 我通过单声道运行 但我不断收到此错误 Unhandled Exception System Exception Connection lost at
  • 需要哪个版本的 Visual C++ 运行时库?

    microsoft 的最新 vcredist 2010 版 是否包含以前的版本 2008 SP1 和 2005 SP1 还是我需要安装全部 3 个版本 谢谢 你需要所有这些
  • 控制到达非 void 函数末尾 -wreturn-type

    这是查找四个数字中的最大值的代码 include
  • 如何让Gtk+窗口背景透明?

    我想让 Gtk 窗口的背景透明 以便只有窗口中的小部件可见 我找到了一些教程 http mikehearn wordpress com 2006 03 26 gtk windows with alpha channels https web
  • 32 位到 64 位内联汇编移植

    我有一段 C 代码 在 GNU Linux 环境下用 g 编译 它加载一个函数指针 它如何执行并不重要 使用一些内联汇编将一些参数推送到堆栈上 然后调用该函数 代码如下 unsigned long stack 1 23 33 43 save
  • 如何使用 std::string 将所有出现的一个字符替换为两个字符?

    有没有一种简单的方法来替换所有出现的 in a std string with 转义 a 中的所有斜杠std string 完成此操作的最简单方法可能是boost字符串算法库 http www boost org doc libs 1 46

随机推荐

  • PyCharm专业版破解

    0x01 下载JetbrainsCrack的jar包 下载链接 链接 百度云链接 提取码 8u4c 0x02 把JetbrainsCrack的jar包放入pycharm文件下的bin目录中 0x03 加上必要的文件代码 在bin目录下使用记
  • 题11:最短摘要的生成

    题目 Alibaba笔试题 给定一段产品的英文描述 包含M个英文单词 每个英文单词以空格分隔 无其他标点符号 再给定N个英文单词关键字 请说明思路并编程实现方法 String extractSurmary String descriptio
  • crout分解计算例题_化学方程式计算你真学会了吗?

    先看看视频 听听姚老师教的计算步骤吧 例题1 加热分解6 3g高锰酸钾 可以得到多少克氧气 分析 这道题是已知反应物的质量来求生成物的质量 即已知原料的质量求产品的质量 我们一起来看课本中的解题步骤 解 设加热分解6 3g高锰酸钾 可以得到
  • Java学习——String

    在上一篇我们讲到了一个必要重要的知识点 那就是Java中的类和对象 我们可以点击下面的链接来进行复习 CSDNJava学习 类和对象 上 AlwaysBeMyself的博客 CSDN博客CSDN 今天我们来讲下一个知识点 那就是Java中的
  • shift+空格,英文字母间距变大,半角全角转换

    shift 空格半角全角快捷键
  • 一文读懂舵机工作原理并运用(附代码)

    杂谈 自己拿到这一模块是也挺迷茫的 后来看了一些资料 也渐渐积累了些自己的理解 很多博文并没有将舵机讲明白 至少你待把PWM与角度如何换算讲清楚吧 所以笔者写这篇博文供大家学习掌握 如果你拿到一个舵机 该咋办 莫慌 往下看 第一步先要区分这
  • 【机器学习】树模型决策的可解释性与微调(Python)

    一 树模型的解释性 集成学习树模型因为其强大的非线性能力及解释性 在表格类数据挖掘等任务中应用频繁且表现优异 模型解释性对于某些领域 如金融风控 是极为看重的 对于树模型的解释性 我们常常可以通过输出树模型的结构或使用shap等解释性框架的
  • Jave Web 03 Cookie、Session

    1 会话 一个网站 如何证明你来过 客户端 服务端 服务端给客户端一个 信件 客户端下次访问服务端带上信件就可以了 cookie 服务器登记你来过了 下次你来的时候我来匹配你 seesion 2 保存会话的两种技术 1 cookie 客户端
  • Python爬虫入门教程!手把手教会你爬取网页数据

    其实在当今社会 网络上充斥着大量有用的数据 我们只需要耐心的观察 再加上一些技术手段 就可以获取到大量的有价值数据 这里的 技术手段 就是网络爬虫 今天就给大家分享一篇爬虫基础知识和入门教程 什么是爬虫 爬虫就是自动获取网页内容的程序 例如
  • 详解STM32 GPIO口中的推挽输出和开漏输出

    推挽输出 GPIO引脚线路经过两个保护二极管后 向上流向 输入模式 结构 向下流向 输出模式 结构 先看输出模式部分 线路经过一个由P MOS和N MOS管组成的单元电路 这个结构使GPIO具有了 推挽输出 和 开漏输出 两种模式 所谓的推
  • EduCoder_web实训作业--JavaScript学习手册九:字符串的常用方法

    第一关 请在此处编写代码 Begin var c a indexOf b var sum 0 while c gt 0 sum c c a indexOf b c b length return sum End 第二关 请在此处编写代码 B
  • jquery动态给下拉框select添加option

    jquery动态给下拉框select添加option 注意 有的框架如layui 需要额外添加form render 否则会失效
  • 华为ENSP的Stelnet、直连、串口连接、telnet连接登录

    华为ENSP设备登录的几种方式 一 直接打开终端窗口 启动设备后 直接双击设备即可 如下图所示 二 用ENSP中的PC连接线CTL到设备的console登录 步骤1 在左侧的连线中找到CTL线单击 如果没有CTL线说明ENSP的版本太低 如
  • 如何打印2019年每个月的第一个星期天的日期

    这是一个关于日期处理的题目 在这里我主要用了Calendar类的相关属性与方法 首先 我们来分析一下 我们可以从2019年1月1日 用while循环依次遍历 到2019年12月7日结束 每次加一天 利用if判断 满足在第一周且是周日的条件
  • 电脑重装系统后无法连接网络怎么处理

    最近小编的台式电脑重装系统后无法连接网络 发现很多朋友也有同样的情况 那么遇到这种情况我们要如何处理呢 下面小编就来为大家讲解一下台式电脑重装系统后无法连接网络处理方法 方法 步骤 方法一 检查本地连接问题 1 鼠标右键点击系统桌面上的 网
  • DocArray 0.20.0 发布!新增 Milvus 后端支持,更好地嵌套数据搜索,新增 RGB-D 格式的 3D 模型表示

    DocArray 是一个用于处理 传输和存储多模态数据的 Python 工具包 DocArray 提供便捷的多模态数据处理功能 具备基于 Protobuf 提供高性能的网络传输性能 同时也为多种向量存储方案提供统一的 API 接口 GitH
  • CTFShow-Web入门

    目录 爆破 web21 web22 web23 web24 web25 web26 web27 web28 爆破 web21 解题思路 考察burp的intruder模块 访问发现是前端验证 随便输入账号密码抓包发现是Basic认证 Bas
  • 【splishsplash】splishsplash入门使用

    本文的目地为总结splishsplash的入门使用方法 splishsplash是一个C 开源流体引擎 主要用于产生流体动画 它的核心算法是SPH法 资源汇总 github https github com InteractiveCompu
  • 数据挖掘之关联规则挖掘的一些定义

    一 算法定义 关联规则挖掘用于发现隐藏在大型数据集中的令人感兴趣的联系 所发现的模式通常用关联规则或频繁项集的形式表示 关联规则反映了一个事物与其他事物之间的相互依存性和关联性 如果两个或多个事物之间存在一定的关联关系 那么 其中一个事物发
  • c++ STL map简介

    首先头文件 include