map的三种插入方法和常用函数以及一些小问题

本文介绍了C++中如何使用map数据结构,包括对一维和多维键值对的操作,展示了如何通过`a[]`, `b[]`, `c[]`来访问和输出不同类型的键值对。重点在于`p.x`, `p.y`, `p.x.x`, `p.x.y`的使用实例和换行的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second

map<int, int> a;
map<pair<int, int>, int > b;
map<int, pair<int, int> > c;

int main()
{
	for (int i = 1; i <= 3; i ++ ) 
	{
		a[i] = i;
		b[{i, i}] = i + 3;
		c[i] = {i + 6, i + 6};
	}
	
	cout << 'a' << endl;//char型字符a
	for (auto p : a)
	{
		cout << p.x << ' ' << p.y << endl;//空格' '
	}
	
	cout << 'b' << endl;
	for (auto p : b)
	{
		cout << p.x.x << ' ' << p.x.y << ' ' << p.y << endl;
	}
	
	cout << 'c' << endl;
	for (auto p : c)
	{
		cout << p.x << ' ' << p.y.x << ' ' << p.y.y;
        puts("");//换行
	}
}

总结:用map读入的话,p.x, p.y,甚至p.x.x, p.x.y ,都行,换行可以endl 可以puts("");

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值