2012-01-10 18 views
1

C++でハッシュのハッシュを作成する方法はありますか?C++でハッシュのハッシュを作成するには?

効果的に私はあなたがPerlでできることをしようとしていますが、C++でしかできません。ここ 、すなわち位置がgameobject1に存在するかもしれないが、存在しないかもしれない...私は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/] 
}, 
); 

ものを持っていると思いPerlのコードの例であるゲームオブジェクトのあるハッシュが存在することが可能ですかゲームオブジェクト35。

アイデア?

+0

私はあなたの質問の "c/C++"を "C++"に置き換えました。なぜならCとC++は異なる言語であるからです。私の前提が間違っていて実際にはCのソリューションに興味があるのなら、私を修正してください。 –

+0

私はどちらかに行き着くか、それともこれまでに感銘を受けました:) C++の作品 – vternal3

答えて

4

Perlのハッシュを使用すると、値のために何を使用してみましょう。 C++は静的に型指定された言語であるため、それを行うことはできません。ハッシュの値(C++のlingoではマップ)にどの型を指定するかを正確に指定する必要があります。あなたは本当にあなたが得るためにstd::map<std::string, boost::any>を使用することができ、PerlはPerlのハッシュのようなものがハッシュたい場合は、いくつかの強い型付けがで:)

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

投げでここ

は、C++ 11とブーストで可能なソリューションですマップに何かを格納する能力。ただし、マップから取得する前に、すべての値の型をテストする必要があります。特定の種類のセットのみが可能な場合は、boost::anyより厳密に型指定されたもの、たとえばboost::variantを使用できます。

+0

これをコンパイルしようとしましたか? 'hash {'でエラーが発生します。エラーC2470: 'hash':関数定義のようですが、パラメータリストはありません。見た目のボディ をスキップし、 'dims {2,4}、' と 'sound_map { {" attack "、{player_attack.ogg"}}、 ' – vternal3

+0

@ vternal3:はい、これはGCC 4.7で正常にコンパイルされます。私が答えで述べたように、これはC++ 11の機能を使用するので、かなり最近のコンパイラが必要です。 –

+0

本当にありがとうございます!あなたは揺れる – vternal3

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; 
+0

これは良いlitteral翻訳ですが、それは非常に悪い性能を持つかもしれません。可能であれば、私はいくつかの表現を考えます。 –

+0

彼の質問は、ハッシュのハッシュを作成する方法でした。しかし、私は同意する、プロパティを持つクラスを使用する方が良い方法だろう。 – Naddiseo

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

乾杯... :)

関連する問題