gpt4 book ai didi

c++ - 使用共享指针 vector 访问 map 中的元素

转载 作者:行者123 更新时间:2023-11-30 04:33:24 25 4
gpt4 key购买 nike

我的情况有些特殊,无法正常工作。

我遵循了很多使用 maps of maps 的示例,但共享指针的 vector 似乎让我有点困惑。

假设我有以下内容:

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);

是吗?我没有收到任何编译/运行时错误...但是由于我还没有弄清楚如何访问 map 的所有不同元素,所以我无法确认这一点!

这是config结构:

struct Config
{
std::string actType;
std::string mapDate;

// Map
ActivityMap actMap;
InnerActivityMap innerActMap;

//Iterator
ActivityMap::iterator actMapIter;
InnerActivityMap::iterator innerActMapIter;
};

现在,假设我想访问 ActivityMap 的每个元素。我将如何获得以下元素?

外层 map Key?

for (config.actMapIter= config.actMap.begin();
config.actMapIter != config.actMap.end();
++config.actMapIter)
{
std::cout << "Outer Key = "
<< (*config.actMapIter).first << std::endl;
}

这似乎可以解决问题。

内图Key?我想不通。

内部 map vector 元素?

如果我知道这两个键,我可以这样做:

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;
}
}
}

我没有收到任何错误,但我只将外键打印到屏幕上:

Outer Key = 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;
}
}
}

我将以下内容打印到屏幕上:

Outer Key = M

Inner Key = 2010/07

iHousehold = 50

iZero = 150

最佳答案

遍历 map 时,.first 是“键”,.second 是属于该键的数据。

所以在你的情况下:

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
}

关于c++ - 使用共享指针 vector 访问 map 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6901934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com