2012-01-10 3 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/] 
}, 
); 

일을하고 싶은 펄 코드의 예입니다 게임 오브젝트에와 해시가 존재할 수인지 gameobject35 용.

아이디어가 있으십니까?

+0

C 및 C++이 다른 언어이기 때문에 질문에서 "c/C++"를 "C++"로 바꿨습니다. 제 가정이 잘못되어 실제로 C 솔루션에 관심이 있다면 저를 고치십시오. –

+0

나는 어느 쪽이든 또는 어떤 감각을 가지고 일종의 갔다 :) C++ 작품 – vternal3

답변

4

펄 해시는 값 아무것도 사용할 수 있습니다. C++은 정적으로 타입 된 언어이므로, 그렇게하지 못하게 될 것입니다 : 여러분은 해시 (C++ 용어, 맵에서)에 가질 값의 타입을 정확하게 지정해야합니다. 당신이 정말로, 당신이 얻을 std::map<std::string, boost::any>를 사용하여 해쉬가 펄의 펄 해시 같은 것을 원하는 경우

여기에 :)

#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::variant과 같이 boost::any보다 강력한 형식의 것을 사용할 수 있습니다.

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

건배 ... :)

관련 문제