c++ map和vector嵌套混合使用
C++ vector和map的嵌套使用_探索鸭的博客-CSDN博客_c++ map 嵌套vector
注意:插入之前先find一下,如果迭代器指向end(),则之前没有相应的key,先加入key,否则直接在相应的key下操作【map】。
另外:
- Multimap允许重复元素,map不允许重复。因此Multimap不能按key访问,find函数将返回第一个找的key所在迭代器。
- 它们可以根据 key 的排序准则自动将元素排序,集合中的元素按一定的顺序排列。元素插入过程是按排序 规则插入,所以不能指定插入位置。
-
std::map<std::string, std::vector<std::string> > StringVecMap;
-
-
std::string key;
-
-
std::string value;
-
-
-
// 插入
-
-
StringVecMap::iterator it = StringVecMap.find(user_name);
-
-
if (it == StringVecMap.end()) {
-
-
std::vector<std::string> vec;
-
-
vec.push_back();
-
-
StringVecMap.insert(make_pair(key, vec));
-
-
}else
-
-
it->second.push_back(value);
-
-
-
// 想做到以key为序的遍历输出也非常简单
-
-
for (it = StringVecMap.begin(); it != StringVecMap.end(); ++it) {
-
-
vector<string>::iterator it_inner;
-
-
for (it_inner = it->second.begin(); it_inner != it->second.end(); ++it_inner) {
-
-
std::cout << *it << std::endl;
-
-
}
-
-
}
Multimap只能按照迭代器完成输出
-
for (it=multimap.begin(); it!=multimap.end(); ++it)
-
std::cout << it->first << " => " << it->second << '\n';
以下内容转自:https://blog.csdn.net/qq_42852337/article/details/108250024
注意: 在一个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;
}
}
文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/126002512
- 点赞
- 收藏
- 关注作者
评论(0)