访问带有共享指针向量的映射的映射中的元素
Accessing elements in maps of maps with vectors of shared pointers
我有一个特殊的情况,我不能很好地工作。
我已经学习了很多使用映射的映射的例子,但是共享指针的向量似乎让我有点迷惑。
假设我有以下内容:
typedef boost::shared_ptr<RecCounts> RecCountsPtr;
typedef std::vector<RecCountsPtr> RecCountsPtrVec;
typedef std::map<std::string, RecCountsPtrVec> InnerActivityMap;
typedef std::map< std::string, InnerActivityMap > ActivityMap;
其中RecCounts
为简单结构。
现在,我想我已经知道如何正确地填充我的ActivityMap
了。
RecCountsPtr recCountsPtr(new RecCounts());
config.actType = "M";
config.mapDate = "2010/07";
recCountsPtr->iHousehold = "50";
recCountsPtr->iZero = "150";
config.actMap[config.actType][config.mapDate].push_back(recCountsPtr);
是吗?我没有得到任何编译/运行时错误为这个…但由于我还没有弄清楚如何访问地图的所有不同元素,我不能确认这一点!
config
结构:
struct Config
{
std::string actType;
std::string mapDate;
// Map
ActivityMap actMap;
InnerActivityMap innerActMap;
//Iterator
ActivityMap::iterator actMapIter;
InnerActivityMap::iterator innerActMapIter;
};
现在,假设我想访问ActivityMap
的每个元素。如何得到以下元素?
外映射键?
for (config.actMapIter= config.actMap.begin();
config.actMapIter != config.actMap.end();
++config.actMapIter)
{
std::cout << "Outer Key = "
<< (*config.actMapIter).first << std::endl;
}
这似乎奏效了。
内部映射键?我想不明白。
内部映射向量元素?
我可以这样做,如果我知道两个键:
config.actMap[config.actType][config.mapDate][0]->iHouehold
config.actMap[config.actType][config.mapDate][0]->iZero
…但似乎不知道如何遍历它们。(
这是我尝试遍历所有元素所做的。
for (config.actMapIter= config.actMap.begin();
config.actMapIter != config.actMap.end();
++config.actMapIter)
{
std::cout << "Outer Key = " << (*config.actMapIter).first << std::endl;
for (config.innerActMapIter = config.innerActMap.begin();
config.innerActMapIter != config.innerActMap.end();
++config.innerActMapIter)
{
std::cout << "Inner Key = "
<< (*config.innerActMapIter).first << std::endl;
for (size_t i = 0;
i < config.actMap[(*config.actMapIter).first]
[(*config.innerActMapIter).first].size();
++i)
{
std::cout << "iHousehold = "
<< config.actMap[(*config.actMapIter).first]
[(*config.innerActMapIter).first]
[i]->iHousehold << std::endl;
std::cout << "iZero = "
<< config.actMap[(*config.actMapIter).first]
[(*config.innerActMapIter).first]
[i]->iZero << std::endl;
}
}
}
我没有得到任何错误,但我只得到外键打印到屏幕:
外键= M
我怀疑我的内部迭代器有问题…因为它与ActivityMap
没有关联。即使我是对的,我也不知道如何建立这样的联系。
有什么建议吗?
ANSWER (thanks to crashmstr):
下面是由crashmstr给出的答案的详细版本。
for (config.actMapIter= config.actMap.begin();
config.actMapIter != config.actMap.end();
++config.actMapIter)
{
std::cout << "Outer Key = " << (*config.actMapIter).first << std::endl;
InnerActivityMap innerActMap = (*config.actMapIter).second;
InnerActivityMap::iterator innerActMapIter;
for (innerActMapIter = innerActMap.begin();
innerActMapIter != innerActMap.end();
++innerActMapIter)
{
std::cout << "Inner Key = " << (*innerActMapIter).first << std::endl;
for (size_t i = 0;
i < config.actMap[(*config.actMapIter).first][(*innerActMapIter).first].size();
++i)
{
std::cout << "iHousehold = "
<< config.actMap[(*config.actMapIter).first]
[(*innerActMapIter).first]
[i]->iHousehold << std::endl;
std::cout << "iZero = "
<< config.actMap[(*config.actMapIter).first]
[(*innerActMapIter).first]
[i]->iZero << std::endl;
}
}
}
我得到以下内容打印到屏幕上:
外键= M
内键= 2010/07
iHousehold = 50
iz0 = 150
当在map上迭代时,.first
是"key", .second
是属于该key的数据。
所以在你的例子中:
for (config.actMapIter= config.actMap.begin();
config.actMapIter != config.actMap.end();
++config.actMapIter)
{
std::cout << "Outer Key = " << (*config.actMapIter).first << std::endl;
//(*config.actMapIter).second is a std::map<std::string, RecCountsPtrVec>
//create a new for loop to iterate over .second
}
相关文章:
- 如何从存储在std::映射中的std::集中删除元素
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 删除映射和分割错误中的一个过去结束元素
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射
- 访问映射向量的映射元素(c++)
- 为什么 unordered_map::equal_range upper_bound 如果键小于第一个映射元素,则返回
- 根据类成员对同一密钥的多映射元素进行排序
- C++查找并擦除多映射元素
- C++ for_each循环找不到指向映射元素
- 创建映射元素时,请避免使用值副本
- 从常量迭代器到映射向量,获取映射向量中映射元素的键和值
- c++中如何更改映射元素的值
- 传递对映射元素的引用作为参数
- 如果没有找到映射元素将返回什么
- 不能访问指针-向量映射元素
- 存储内部排序映射元素的最快方式
- 无序多映射元素插入
- 哈希映射元素的值未正确打印
- 修改所有映射元素的键
- 复制映射元素的正确方法