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

  • SEARCH
  • Home
  • 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 7848975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:23:15+00:00 2026-06-02T18:23:15+00:00

typedef boost::unordered_map<int, void*> OneDimentionalNodes; typedef boost::unordered_map<int, OneDimentionalNodes> TwoDimentionalNodes; TwoDimentionalNodes nodes; is this valid? i

  • 0
typedef boost::unordered_map<int, void*> OneDimentionalNodes;
typedef boost::unordered_map<int, OneDimentionalNodes> TwoDimentionalNodes;

TwoDimentionalNodes nodes;

is this valid?

i don’t use any hash functions since keys of the unordered_maps’ are single integers.
it compiles, but when i iterate it like this, it crashes while trying to access this->hash_function()(k);

for (TwoDimentionalNodes::iterator it= nodes.begin(); it != nodes.end() ; ++it)
{
   for(OneDimentionalNodes::iterator it2 = nodes[it->first].begin(); it2 != nodes[it->first].end() ; ++it2)
    {
   // do stuff
    }
}

i’m also open to other containers with

  • O(1) access
  • O(n) iteration
  • Sparse
  • 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-06-02T18:23:17+00:00Added an answer on June 2, 2026 at 6:23 pm

    If you just need to iterator over all elements, and it is not required to loop over a specific dimension, then you could use a simple pair as key for your unordered_map, like this:

    typedef std::pair<int,int> Coordinates;
    typedef std::unordered_map<Coordinates,void *> TwoDimensionalNodes;
    

    (notice I used STL instead of Boost, unordered_map is now also part of the standard STL).

    Getting a specific value is simply writing:

    twoDimensionalNodes[std::make_pair(x,y)]
    

    (or use find if you’re not sure if that value is in your map).

    To iterate, just iterate over the unordered map:

    for (auto it=twoDimensionalNodes.begin();it!=twoDimensionalNodes.end();++it)
       {
       std::cout << "x=" << it->first.first;
       std::cout << "y=" << it->first.second;
       std::cout << "value=" << it->second;
       }
    

    To make it a bit more readable, I prefer getting the coordinates first from the iterator, like this:

    for (auto it=twoDimensionalNodes.begin();it!=twoDimensionalNodes.end();++it)
       {
       Coordinates &coordinates = it->first;
       std::cout << "x=" << coordinates.first;
       std::cout << "y=" << coordinates.second;
       std::cout << "value=" << it->second;
       }
    

    If you have more than 2 dimensions, use std::tuple, or simply write your own Coordinates class to be used as key for the map.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this iterator loop, typedef boost::unordered_map<std::pair<int, int>, NavigationNode> NodesMap; NodesMap nodes; for (NodesMap::iterator
void main() { typedef boost::ptr_map<int, char> MyMap; //typedef std::map<int, char *> MyMap; // in
I have such situation in boost and C++: // Somewhere: typedef boost::signals2::signal<void (int KeyCode)>
I've defined some signal: typedef boost::signals2::signal<void (int temp)> SomeSig; typedef SomeSig::slot_type SomeSigType; I have
I have a boost variant of looking like this: typedef boost::variant<int, float, double, long,
I'm writing a hash functor for use in a boost::unordered_map which will store boost::graph
I'm using boost::function for making function-references: typedef boost::function<void (SomeClass &handle)> Ref; someFunc(Ref &pointer) {/*...*/}
I wrote a small tcp client using boost::asio, providing the following function: typedef boost::function<void(const
I have a boost::mpl::vector with N elements, say: typedef boost::mpl::vector<int,float,double,short,char> my_vector; I wish to
I have the following rule in boost::spirit : typedef boost::tuple<int, int> Entry; qi::rule<Iterator, Entry(),

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.