C++ 二维、三维 map 的使用

15 篇文章 0 订阅

前言

最近复习了一下 map 的使用,因为使用的都是一维的,所以使用常规操作就能满足需求。以前看到过有人在工作环境中使用多维度的 map,当时没有尝试实现这种需求,今天想起来了就试了下。类似于 map<string, map<string, string>> 这种形式。常规操作是对数据的增删查改

二维 map

创建一个二维map,这里以string为例:

map<string, map<string, string>> b;

增加元素

增加元素可分为两种情况:(1) 增加若干个数据时可以使用 map 中的 insert() 方法,或者 insert_or_assign() 方法。**两者的区别:使用insert()方法时,若相应key不存在,则完成插入操作。反之,则不做插入操作。**使用 insert_or_assign() 方法时,若相应 key 不存在,则完成插入操作。反之,完成赋值操作。

(2) 增加单独的数据可以使用赋值方法test[ “维度1”][ “维度2”] = "字符串 ";,这种方法既简单又方便。若原容器中没有相应的 key ,则完成插入操作,反之完成赋值操作。

for(int i = 0; i < 3; i++)
{
	temp = to_string(i);
	c.insert(make_pair(temp, "string"));
}

//将含有3个元素的 map c 插入到 map b中
b.insert(make_pair(temp, c));

//添加一个元素
b["2"]["1"] = "123456";

删除元素

使用 map 中的 erase() 方法删除容器中的一个值或某一维度下的全部值 (删除键为 key 的若干条数据) 。

b["2"].erase("1");
在第一个维度 key 为 "2" 的前提下,删除第二个维度 key 为 "1" 的值。

b.erase("1");
表明删除第一个维度 key 为 "1" 的所有的值。

查找与修改

简单的查找可以使用 map 中的 find() 完成,这里主要介绍遍历时的操作。遍历是最常用的查找操作,在遍历过程中,满足一定条件后完成相应的操作。修改操作可以使用赋值语句完成,这里不再赘述。若想对一个二维 map 进行遍历,需要使用两个迭代器,具体请看代码:

void Print(map<string, map<string, string>> a)
{
	map<string, map<string, string>>::iterator p2;
	map<string, string>::iterator p3;

	for(p2 = a.begin(); p2 != a.end(); p2++)
	{
		for(p3 = p2->second.begin(); p3 != p2->second.end(); p3++)
			cout << p2->first << ":" << "[" <<p3->first << "]" << "[" << p3->second << "]" <<endl;
	}
	cout <<endl;
}

修改容器中特定的数据,可以进行遍历,然后对满足条件的元素完成操作,其做法类似于一维 map 的操作:

#include <map>
#include <iostream>
int main()
{
	std::map<int, std::string> c = {{1, "one"}, {2, "two"}, {3, "three"},
		{4, "four"}, {5, "five"}, {6, "six"}};

	for(auto it = c.begin(); it != c.end(); )
		if(it->first % 2 == 1)
			//完成相应操作
		else
			++it;
	for(auto& p : c)
		std::cout << p.second << ' ';
}

完整代码:

#include <iostream>
#include <map>
#include <string>

using namespace std;

void Print(map<string, map<string, string>> a)
{
	map<string, map<string, string>>::iterator p2;
	map<string, string>::iterator p3;

	for(p2 = a.begin(); p2 != a.end(); p2++)
	{
		for(p3 = p2->second.begin(); p3 != p2->second.end(); p3++)
			cout << p2->first << ":" << "[" <<p3->first << "]" << "[" << p3->second << "]" <<endl;
	}
	cout <<endl;
}

int main()
{
	map<string, map<string, string>> b;
	map<string, string> c;
	string temp;

	//对 map c 进行初始化
	for(int i = 0; i < 3; i++)
	{
		temp = to_string(i);
		c.insert(make_pair(temp, "string"));
	}

	//将 map c 插入到 map b 中
	b.insert(make_pair(temp, c));
	cout << "Init:" <<endl;
	Print(b);

	//修改数据
	b["2"]["1"] = "123456";
	b["2"]["2"] = "qweasd";
	cout << "Modified:" <<endl;
	Print(b);

	b["2"].erase("1");
	cout << "Erase:" <<endl;
	Print(b);

	cout << "Add element:" <<endl;
	b["3"]["3"] = "3333";   
	Print(b);

	return 0;
}

三维map

三维 map 的操作和二维 map 的操作相同,只是多了一个维度,这里仅放一个简单的代码作为参考:

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

void Print(map<string, map<string, map<string, string>>> a)
{
	map<string, map<string, map<string, string>>>::iterator p1;
	map<string, map<string, string>>::iterator p2;
	map<string, string>::iterator p3;
	for(p1 = a.begin(); p1 != a.end(); p1++)
	{
		for(p2 = p1->second.begin(); p2 != p1->second.end(); p2++)
		{
			for(p3 = p2->second.begin(); p3 != p2->second.end(); p3++)
				cout << "Row:[ " << p1->first << " ] Column:[ " << p2->first << " ] Timestamp:" << "[ " <<p3->first << " ]" << " ->  [" << p3->second << "]" <<endl;
		}
	}
	cout <<endl;
}

int main()
{
	map<string, map<string, map<string, string>>> a;
	map<string, map<string, string>> b;
	map<string, string> c;
	string temp;
	
	//初始化并插入
	for(int i = 0; i < 2; i++)
	{
		temp = to_string(i);
		c.insert(make_pair(temp, "string"));
		b.insert(make_pair(temp, c));
		a.insert(make_pair(temp, b));
	}
	cout << "Init:" <<endl;
	Print(a);

	//修改
	a["1"]["1"]["0"] = "123456";
	a["1"]["1"]["1"] = "qwerty";
	cout << "Modified:" <<endl;
	Print(a);


	a["1"].erase("1");
	cout << "Erase column == 1:" <<endl;
	Print(a);

	a.erase("1");
	cout << "Erase row == 1:" <<endl;
	Print(a);
	
	return 0;
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值