注意: 在一个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;
	}
}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐