嵌套map的用法【二维map】

1.定义

map<int,map<int,int> > mp1;
map<string,map<string,int> > mp2;

map此时的value值也是一个map对象
赋值:
这是比较直接的赋值操作,相当于是一个二维数组赋值

    mp1[1][2]=5;
	mp2["s1"]["t1"]=7;
	mp2["s2"]["t2"]=8;
	mp2["s3"]["t3"]=9;
	mp2["s4"]["t4"]=10;

2.遍历,两个迭代器,一个外迭代器,一个内迭代器

for(map<string,map<string,int>>::iterator it1=mp2.begin();it1!=mp2.end();it1++)
	{
		cout<<it1->first<<endl;
		for(map<string,int>::iterator it2=it1->second.begin();it2!=it1->second.end();it2++)
		cout<<it2->first<<" "<<it2->second<<endl;
	}		

运行结果:
在这里插入图片描述
洛谷 P3613 寄包柜
在这里插入图片描述
分析:还不会二维map的时候,想到是用结构体,现在直接用上文提到的赋值操作即可
AC代码:

#include<bits/stdc++.h>
using namespace std;
map<int,map<int,int> > mp;
int main()
{
	int n,q;
	int a,b,c,d,e,f;
	cin>>n>>q;
	for(int i=0;i<q;i++)
	{
		cin>>a;
		if(a==1)
		{
		  cin>>b>>c>>d;
		  mp[b][c]=d;		  
		}
		else if(a==2)
		{
			cin>>e>>f;
			cout<<mp[e][f]<<endl;			
		}
	}
}

hdu 1213
水果
在这里插入图片描述
分析:和上题区别不大,这题是计总数,所以计数的时候要累加,一开始还有点卡,没想到应该怎么去计算总数,其实直接在map后叠加就可以了
注意格式,每组数据之间有一个空行,最后一组数据没有空行
AC代码:

#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
map<string,map<string,int> > mp;
int main()
{
	int t,n;
	cin>>t;
    string ch1,ch2;
    int sum;
	while(t--)
	{
		cin>>n;
		mp.clear();
		for(int i=0;i<n;i++)
		{
			cin>>ch1>>ch2>>sum;
			mp[ch2][ch1]+=sum;//因为要计算总数,所以要累加 
		}
		for(map<string,map<string,int> >::iterator it1=mp.begin();it1!=mp.end();it1++)
		{
			cout<<it1->first<<endl;//外迭代器 
			for(map<string,int>::iterator it2=it1->second.begin();it2!=it1->second.end();it2++)
			{
				cout<<"   |----"<<it2->first<<"("<<it2->second<<")"<<endl; //内迭代器 
			}			
		}
		if(t!=0)
		cout<<endl;		
	}
}
### C++ 使用 `std::map` 实现二维数组 在 C++ 中,可以利用标准库容器 `std::map` 来模拟二维数组的行为。相比于传统意义上的固定大小的多维数组,基于 `std::map` 的实现提供了更灵活的方式处理稀疏矩阵或动态变化的数据集。 #### 基本原理 `std::map` 是一种关联式容器,允许存储键值对,并保持按键排序。为了表示二维空间中的位置,可以选择使用一对整型作为复合键来唯一标识每一个可能的位置 `(row, col)` 。对于每个这样的键,对应的值即为该位置上的数据项[^1]。 #### 示例代码 下面是一个简单的例子展示如何通过嵌套映射(pair<int,int> -> T)的形式构建一个类似于二维数组的对象: ```cpp #include <iostream> #include <utility> // For std::pair #include <map> using namespace std; typedef pair<int, int> Coord; typedef map<Coord, int> SparseMatrix; void setElement(SparseMatrix& matrix, int row, int col, int value){ matrix[make_pair(row, col)] = value; } int getElement(const SparseMatrix& matrix, int row, int col){ auto it = matrix.find(make_pair(row, col)); if(it != end(matrix)){ return (*it).second; } throw runtime_error("No such element"); } int main(){ SparseMatrix sm; // 设置一些元素 setElement(sm, 0, 0, 5); setElement(sm, 1, 2, 8); try{ cout << "Value at (0,0): " << getElement(sm, 0, 0) << endl; cout << "Value at (1,2): " << getElement(sm, 1, 2) << endl; // 尝试访问不存在的元素会抛出异常 cout << "Value at (99,99): " << getElement(sm, 99, 99) << endl; }catch(exception& e){ cerr << "Error: " << e.what() << '\n'; } return 0; } ``` 此程序定义了一个名为 `SparseMatrix` 类型用于保存具有两个维度索引的关键字以及相应的数值。函数 `setElement()` 和 `getElement()` 提供了设置和获取特定坐标的值的方法。当尝试读取未被赋过值的位置时将会触发错误提示[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值