【发布时间】:2011-08-01 17:10:07
【问题描述】:
我有一个有点特殊的情况,我无法完全开始工作。
我已经关注了很多使用映射映射的示例,但是共享指针的向量似乎让我有点不知所措。
假设我有以下:
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 的每个元素。我将如何获得以下元素?
外层地图Key?
for (config.actMapIter= config.actMap.begin();
config.actMapIter != config.actMap.end();
++config.actMapIter)
{
std::cout << "Outer Key = "
<< (*config.actMapIter).first << std::endl;
}
这似乎可以解决问题。
内图Key? 我想不通。
内图矢量元素?
如果我知道这两个键,我可以这样做:
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 没有关联。即使我是正确的,我也不知道如何建立这样的关联。
有什么建议吗?
回答(感谢 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
iZero = 150
【问题讨论】:
-
由于您的
Config结构包含活动类型和地图日期,并且您分别将它们用作外部地图和内部地图的键,因此每个Config实例似乎只有 1为外部和内部地图分别设置键。那你为什么需要std::map呢?为什么Config不能只持有一个RecCountsPtrVec实例? -
抱歉,这是我实际使用的非常简化的版本。这两个值将发生变化并用于在地图中创建条目。
标签: c++ map iterator shared-ptr