C++ vector与map的混合运用

注意: 在一个A容器存另一个容器B的指针的时候
map<int, string>* temMap = it->second;//通过A获取到容器B的指针
map<int, string>::iterator itMap = temMap->begin();//创建容器B的迭代器
for (; itMap != temMap->end(); itMap++)//遍历
cout << " 1map key=" << it->first.c_str() << " 2map key " << itMap->first << " value =" << itMap->second.c_str() << endl;// itMap->first 可以取B容器的Key

以下是混合运用实例:
#include
#include
#include
using namespace std;

//1. map 存 vector 数组
map<string, vector<int>> mapVec;
vector<int>  veca;
veca.push_back(1);
veca.push_back(2);
veca.push_back(3);
mapVec.insert(pair<string, vector<int>>("hello", veca));
vector<int>  vecb;
vecb.push_back(100);
vecb.push_back(200);
mapVec.insert(pair<string, vector<int>>("world", vecb));
map<string, vector<int>>::iterator it = mapVec.begin();
for (; it != mapVec.end(); it++)
{
	vector<int> vect = it->second;
	vector<int>::iterator itvct = vect.begin();

	for (; itvct != vect.end(); itvct++)
	{
		cout << " map =" << it->first.c_str() << "  " << *itvct << endl;
	}
}

//2. map 存 vector数组指针

map<string, vector<int>* > mapVecPtr;

vector<int>  veca;
veca.push_back(1);
veca.push_back(2);
veca.push_back(3);
mapVecPtr.insert(pair<string, vector<int>*>("hello", &veca));
map<string, vector<int>* >::iterator it = mapVecPtr.begin();
for (; it != mapVecPtr.end(); it++)
{
	vector<int>* vect = it->second; //取出这个数组

	vector<int>::iterator itvct = vect->begin();//头指针

	for (; itvct != vect->end(); itvct++)
	{
		
		cout << " 11 map =" << it->first.c_str() << "  " << *itvct << endl;
		*itvct = 10000;
	}
}

it = mapVecPtr.begin();
for (; it != mapVecPtr.end(); it++)
{
	vector<int>* vect = it->second; //取出这个数组

	vector<int>::iterator itvct = vect->begin();//头指针

	for (; itvct != vect->end(); itvct++)
	{
		*itvct = 10000;
		cout << " map =" << it->first.c_str() << "  " << *itvct << endl;
	}
}


//3.map 存 map
map<string, map<int, string>> mapMap;

map<int, string> tempMap;
tempMap.insert(pair<int, string>(999,"999"));
tempMap.insert(pair<int, string>(888, "888"));

mapMap.insert(pair<string, map<int, string>>("towmap",tempMap));

mapMap.insert(pair<string, map<int, string>>("oneMap", tempMap));

map<string, map<int, string>>::iterator it = mapMap.begin();

for (; it != mapMap.end();it++)
{
	map<int, string> temMap = it->second;
	map<int, string>::iterator itMap = temMap.begin();
	for (; itMap != temMap.end();itMap++)
	{
		cout << " 1map key=" << it->first.c_str() << "  2map key " << itMap->first<<" value ="<< itMap->second.c_str() << endl;
	}
}


//4. map 存 map指针
map<string, map<int, string>* > mapMap;

map<int, string> tempMap;
tempMap.insert(pair<int, string>(999, "999"));
tempMap.insert(pair<int, string>(888, "888"));

mapMap.insert(pair<string, map<int, string>* >("towmap", &tempMap));


map<string, map<int, string>* >::iterator it = mapMap.begin();

for (; it != mapMap.end(); it++)
{
	map<int, string>* temMap = it->second;
	map<int, string>::iterator itMap = temMap->begin();
	for (; itMap != temMap->end(); itMap++)
	{
		cout << " 1map key=" << it->first.c_str() << "  2map key " << itMap->first << " value =" << itMap->second.c_str() << endl;
	}
}

//5. vector存map
vector<map<string, int>> vectMap;

map<string, int> map1;
map1.insert(pair<string,int>("hello",100));
vectMap.push_back(map1);
map1.insert(pair<string, int>("world", 999));
vectMap.push_back(map1);

for (int i =0;i<vectMap.size();i++)
{
	map<string, int> tempMap = vectMap[i];
	map<string, int>::iterator it = tempMap.begin();
	for (;it != tempMap.end();it++)
	{
		cout << " vector=" << i << " map key =" << it->first.c_str() << " value =" <<it->second<< endl;
	}
}
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值