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

前言

最近複習了一下 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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章