如何在C++;? 有没有方法在C++中创建散列散列?

如何在C++;? 有没有方法在C++中创建散列散列?,c++,hash,hashmap,hash-of-hashes,C++,Hash,Hashmap,Hash Of Hashes,实际上,我正在尝试做你在Perl能做的事情,但是只有C++。 下面是一个Perl代码的示例,我希望在C语言中实现++ %hash = ( gameobject1 => { position => { x_loc => 43, y_loc => 59, } rect_size => { width => 5, height => 3, } collida

实际上,我正在尝试做你在Perl能做的事情,但是只有C++。 下面是一个Perl代码的示例,我希望在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/]
},
);
需要注意的是,游戏对象中的哈希值可以存在也可以不存在。。。i、 e.位置可能存在于gameobject1中,但可能不存在于gameobject35中

有什么想法吗?

使用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;
#包括
#包括
类游戏对象;
typedef std::map object\u map\t;
typedef std::对象映射的映射映射;

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

#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";
    }
}
#包括
#包括
#包括
使用名称空间std;
void main(){
映射散列;
散列[“A”][“A”]=1;
散列[“A”][“B”]=2;
散列[“B”][“A”]=4;
散列[“B”][“B”]=8;
for(map::iterator i=hash[“B”].begin();i!=hash[“B+”].end();i++){

CUT第一个P> Perl hash让你使用任何值。C++是静态类型的语言,它不会让你这样做:你必须精确地指定你想要的类型在哈希(C++ + LINGO,map)中有什么值。 下面是使用C++11和boost的可能解决方案,其中加入了一些强类型:)

#包括
#包括
#包括
#包括
//坐标总是这样的,不是吗?
结构坐标{
int x_loc;
国际奥委会;
};
//维度总是这样的,不是吗?
结构变暗{
整数宽度;
内部高度;
};
//声音映射:每个字符串键映射到一个文件名向量
typedef std::map sound_map;
//项目列表:看起来它只是字符串的集合
typedef std::向量项列表;
//提高可读性的花式名称
枚举可碰撞性:布尔{
可碰撞=真,
不可碰撞=错误
};
//描述游戏对象的结构
结构博弈对象{
//任择职位
boost::可选位置;
//可选的矩形大小
boost::可选的rect_大小;
//假设“false”的意思与“no-collidable key”相同
布尔可碰撞;
//假设“空地图”的含义与“无地图”的含义相同
声音地图声音;
//假设“空向量”的含义与“无向量”相同
项目清单项目;
//如果上述任何假设是错误的,
//Spready boost::可自由选择:)
};
//最后,我们的“散列”值
映射散列{
{“游戏对象1”,
{
合作社{43,59},
dims{5,3},
可对位,//还记得那些花哨的名字吗?
声像图{
{“attack”{“player_attack.ogg”},
{“jump”{“player_attack.ogg”},
{“jump_random”、{“player_jump1.ogg”、“player_jump2.ogg”、“player_jump3.ogg”}
},
项目列表{}
} },
{“游戏2”,
{
协调{24,72},
dims{2,4},
不可碰撞,
声像图{
{“攻击”{“地精攻击.ogg”}
},
物品清单{“剑”、“头盔”、“靴子”}
} },
{“游戏对象25”,
{
boost::无,//无位置
dims{2,4},
不可碰撞,
声像图{
{“攻击”{“地精攻击.ogg”}
},
物品清单{“剑”、“头盔”、“靴子”}
} }
};

如果您确实想要Perl哈希的Perl哈希,您可以使用
std::map
来获得在映射中存储任何内容的能力。但是,这要求您在从映射中获取每个值之前测试其类型。如果只能使用某一组类型,则可以使用比
boost更强的类型:这是一个很好的文本翻译,但是它可能有很差的性能。如果可能的话,我会考虑一些表示。他的问题是如何创建哈希值。但是我同意,使用属性的类是更好的方法。我用“C++”代替了“C++”中的“C/C++”。因为C和C++是不同的语言。如果我的假设是错误的,请改正我,而你实际上对C解决方案感兴趣。我有点倾向于哪一种或哪种是有意义的:C++工作你试图编译这个吗?我在<代码>散列中出错。{
:错误C2470:'hash':看起来像一个函数定义,但没有参数列表;跳过外观体,也跳过
dims{2,4}、
声音映射{{“attack”,“player\u attack.ogg},
@vternal3:是的,它在GCC4.7上编译得很好。正如我在回答中提到的,它使用C++11特性,因此需要一个相当新的编译器。
#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" }
    } }
};