c++ vector和map嵌套使用

使用c++实现构建map嵌套vector的集合
关键在于对 map<…,…>::iterator it 对象的使用

#include <QCoreApplication>
#include <map>
#include <iostream>
#include <vector>

using namespace std;
int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
	vector<vector<string>> result;
		
	//构建一个二维数组
	for(int i = 0;i < 6; i++)
	{
		vector<string> temp;
		int j = i%2;
		string XXX = "XXX";
		string XXX2 = XXX.append(to_string(i));
		string region = "region";
		string region2 = region.append(to_string(j));
		
		temp.push_back(to_string(i));
		temp.push_back(XXX2);
		temp.push_back(to_string(j));
		temp.push_back(region2);
		result.push_back(temp);
	}

	//通过二维数组创建一个嵌套map
	map<string,vector<string>> result_map;
	for(int i = 0;i < result.size();i++)
	{
		string key = result[i][3];
		if(result_map.count(key) == 0)
		{
			vector<string> result_vec;
			result_vec.push_back(result[i][1]);
			result_map.insert({result[i][3],result_vec});
		}
		else
		{
			map<string,vector<string>>::iterator it = result_map.find(key);
			it->second.push_back(result[i][1]);
		}
	}
	return a.exec();
}

构建的一个二维数组
创建成功的map对象

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值