2014-04-01 3 views
3

다른 std :: map 값을 사용하여 std :: map을 만들고 싶습니다.이지도를 임의의 깊이에 중첩 할 수 있기를 원합니다. 나는이 수정 길이 할 간단 알고 있지만 변수 깊이 중 하나를 구축하는 접근 방법이 확실하지가변 깊이의 중첩 된지도를 만드는 방법

std::map < std::string, std::map < std::string, int> > d1; 

//  so the next depth would be. 

std::map < std::string, std::map < std::string, std::map < std::string, int> > > d2; 

: 여기

은 기본 예입니다.

template<typename Key, typename Value, unsigned int N> 
struct VarMapHelper 
{ 
    typedef std::map<Key, typename VarMapHelper<Key, Value, N-1>::type> type; 
}; 

template<typename Key, typename Value> 
struct VarMapHelper<Key, Value, 1> 
{ 
    typedef std::map<Key, Value> type; 
}; 

template<typename Key, typename Value, unsigned int N> 
using VarMap = typename VarMapHelper<Key, Value, N>::type; 

이 좋아 사용 : : 우리가 using 전문 기운으로

+0

이러한 클래스를 사용하는 방법에 대한 예를 보여줄 수 있습니까? – Brian

답변

2

, 우리는 class + typedef와 함께 옛날 방식을 가서 결국 using로 노출해야

VarMap<std::string, int, 3> map; 

1 개를 잘못 사용하면 컴파일러가 충돌하지 않도록 방지하려면 e 클래스를 VarMap<std::string, int, 0>으로 지정하면 다음과 같이 0을 전문으로 제공 할 수 있습니다.

template<typename Key, typename Value> 
struct VarMapHelper<Key, Value, 0> 
{ 
    static_assert(false, "Passing variable depth '0' to VarMap is illegal"); 
};