如何在C ++中创建哈希哈希?

时间:2012-01-10 05:21:42

标签: c++ hash hashmap hash-of-hashes

有没有办法在C ++中创建哈希哈希?

实际上,我正在尝试用Perl做你能做的事,但只能用C ++做。 这是我希望在C ++中实现的Perl代码示例

%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/]
},
);

需要注意的是游戏对象中的哈希值是否存在...即游戏对象中可能存在位置但游戏对象可能不存在。

有什么想法吗?

3 个答案:

答案 0 :(得分:6)

Perl哈希允许您使用任何值来表示值。 C ++是一种静态类型语言,它不会让你这样做:你必须准确指定你想要的哈希值(在C ++术语,地图中)的类型。

这是C ++ 11和boost的可能解决方案,其中包含一些强类型:)

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

如果你真的想要像Perl哈希的Perl哈希那样,你可以使用std::map<std::string, boost::any>来在地图中存储任何东西。但是,这需要您在从地图获取每个值之前测试它们的类型。如果只有一组特定类型,您可以使用比boost::any更强类型的内容,例如boost::variant

答案 1 :(得分:2)

使用std :: map?

类似的东西:

#include <map>
#include <string>
class GameObject;
typedef std::map<std::string, GameObject> object_map_t;
typedef std::map<std::string, object_map_t> map_of_object_maps_t;

答案 2 :(得分:2)

此示例可以帮助您了解实现:

#include <map>
#include <string>
#include <iostream>
using namespace std;

void main() {

    map<string, map<string, int>> hash;
    hash["A"]["A"] = 1;
    hash["A"]["B"] = 2;
    hash["B"]["A"] = 4;
    hash["B"]["B"] = 8;

    for(map<string, int>::iterator i = hash["B"].begin(); i != hash["B+"].end(); i++) {
        cout << i->first << " - " << i->second << "\n";
    }
}

干杯...:)