访问带有共享指针向量的映射的映射中的元素

Accessing elements in maps of maps with vectors of shared pointers

本文关键字:映射 元素 向量 指针 共享 访问      更新时间:2023-10-16

我有一个特殊的情况,我不能很好地工作。

我已经学习了很多使用映射的映射的例子,但是共享指针的向量似乎让我有点迷惑。

假设我有以下内容:

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
}