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 7034077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:08:33+00:00 2026-05-28T01:08:33+00:00

Is there a way to create a hash of hashes in C++? Effectively I

  • 0

Is there a way to create a hash of hashes in C++?

Effectively I am trying to do what you can do in Perl but only in C++.
Here is an example of Perl code I would like to have happen in C++

%hash = (
gameobject1 => {
    position => {
        x_loc => 43,
        y_loc => 59,
    }
    rect_size => {
        width => 5,
        height => 3,
    }
    collidable => 1,
    sounds => {
        attack => "player_attack.ogg",
        jump => "player_jump1.ogg",
        jump_random => [qw/player_jump1.ogg player_jump2.ogg player_jump3.ogg/]
    }

},
gameobject2 => {
    position => {
        x_loc => 24,
        y_loc => 72,
    }
    rect_size => {
        width => 2,
        height => 4,
    }
    sounds => {
        attack => "goblin_attack.ogg",
    }
        items => [qw/sword helmet boots/]
},
);

The thing to note is the hashes with in gameobjects can exist or not… i.e. position might exist in gameobject1 but may not exist for gameobject35.

Any ideas?

  • 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-28T01:08:33+00:00Added an answer on May 28, 2026 at 1:08 am

    Perl hashes let you use anything for values. C++ being a statically typed language, it won’t let you do that: you have to specify exactly what type you want the values in the hash (in C++ lingo, the map) to have.

    Here’s possible solution with C++11 and boost, with some strong typing thrown in 🙂

    #include <map>
    #include <vector>
    #include <string>
    #include <boost/optional.hpp>
    
    // Coordinates are always like this, aren't they?
    struct coords {
        int x_loc;
        int y_loc;
    };
    
    // Dimensions are always like this, aren't they?
    struct dims {
        int width;
        int height;
    };
    
    // Sound maps: each string key maps to a vector of filenames
    typedef std::map<std::string, std::vector<std::string>> sound_map;
    // Item lists: looks like it's just a collection of strings
    typedef std::vector<std::string> item_list;
    
    // Fancy names to improve readability
    enum collidability : bool {
        collidable = true,
        not_collidable = false
    };
    
    // A structure to describe a game object
    struct game_object {
        // An optional position
        boost::optional<coords> position;
        // An optional rectangle size
        boost::optional<dims> rect_size;
        // Assuming "false" can mean the same as "no collidable key"
        bool collidable;
        // Assuming an "empty map" can mean the same as "no map"
        sound_map sounds;
        // Assuming an "empty vector" can mean the same as "no vector"
        item_list items;
        // If any of the above assumptions is wrong,
        // sprinkle boost::optional liberally :)
    };
    
    // Finally, values for our "hash"
    std::map<std::string, game_object> hash {
        { "game_object1",
          {
            coords { 43, 59 },
            dims { 5, 3 },
            collidable, // remember those fancy names?
            sound_map {
                 { "attack", { "player_attack.ogg" } },
                 { "jump", { "player_attack.ogg" } },
                 { "jump_random", { "player_jump1.ogg", "player_jump2.ogg", "player_jump3.ogg" } }
            },
            item_list {}
        } },
        { "game_object2",
          {
            coords { 24, 72 },
            dims { 2, 4 },
            not_collidable,
            sound_map {
                 { "attack", { "goblin_attack.ogg" } }
            },
            item_list { "sword", "helmet", "boots" }
        } },
        { "game_object25",
          {
            boost::none, // no position
            dims { 2, 4 },
            not_collidable,
            sound_map {
                 { "attack", { "goblin_attack.ogg" } }
            },
            item_list { "sword", "helmet", "boots" }
        } }
    };
    

    If you really want something like a Perl hash of Perl hashes, you can use std::map<std::string, boost::any> to get the ability to store anything in the map. However, this requires you to test for the types of every value before obtaining it from the map. If only a certain set of types is possible, you can use something more strongly-typed than boost::any, like boost::variant.

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

Sidebar

Related Questions

Is there any way to create a dynamic Pinterest button? I'm trying to add
Is there any way to create a hash of string at compile time using
I have a hash PAYMENT_TYPES_HASH . Is there a way to create an array
Is there any way to use a constant as a hash key? For example:
Is there a way to create a callback function that fires when a list
Is there a way to create a layout like the notification bar (the sliding
Is there a way to create a StringBuilder from a byte[] ? I want
Is there a way to create a NuGet package where when the package is
Is there any way to create breakpoints in clojurescript? Either in the repl or
Is there a way to create a temporary table using the RMySQL package? If

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.