Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6230149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:41:46+00:00 2026-05-24T09:41:46+00:00

I have a somewhat unique situation that I can’t quite get working. I’ve followed

  • 0

I have a somewhat unique situation that I can’t quite get working.

I’ve followed a lot of examples of using maps of maps but the vector of shared pointers seems to throw me off a bit.

Suppose I have the following:

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;

Where RecCounts is a simple structure.

Now, I think I’ve figured out how to populate my ActivityMap properly.

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

Yes? I don’t get any compile/runtime errors for this…but since I haven’t figured out how to access all the different elements of the map I can’t confirm this!

This is the config structure:

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

    // Map
    ActivityMap actMap;
    InnerActivityMap innerActMap;

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

Now, suppose I want to access each element of the ActivityMap. How would I get the following elements?

The outer map Key?

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

This seemed to do the trick.

The inner map Key?
I can’t figure this out.

The inner map vector elements?

I can do it like this if I know the two keys:

config.actMap[config.actType][config.mapDate][0]->iHouehold
config.actMap[config.actType][config.mapDate][0]->iZero

…but can’t seem to figure out how to iterate through them. 🙁

This is what I’ve done to try and iterate through all elements.

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

I don’t get any errors but I only get the outer key print to the screen:

Outer Key = M

I suspect something is amiss with my inner iterator…in that it’s not associated the ActivityMap. Even if I am correct I don’t know how to make such an association.

Any suggestions?

ANSWER (thanks to crashmstr):

Here is a verbose version of the answer suggested by 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;
        }
    }
}

I get the following printed to the screen:

Outer Key = M

Inner Key = 2010/07

iHousehold = 50

iZero = 150

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:41:47+00:00Added an answer on May 24, 2026 at 9:41 am

    When iterating over a map, .first is the “key”, and .second is the data that belongs to that key.

    So in your case:

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I'm in somewhat of a unique situation: I have a decent amount
I have somewhat of a unique situation, if I had a form with a
Ok what I would like to do is somewhat unique, and I can't find
Given a table of models 'A', that can have multiple child models 'B', of
I have a PDF that is being generated from HTML source using HTMLDOC. While
I've got a somewhat unique problem due to our infrastructure. I have two offline
I have a somewhat unique request. What I am looking to do is listen
The title is somewhat self explanatory. I have an entity that has another key
UPDATE: I have somewhat resolved the issue. Just in case if anyone runs in
I need to extract a value from a hidden HTML field, have somewhat figured

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.