2017-04-02 6 views
0
  1. (왼쪽 & 오른쪽)으로 액세스 할 수있는 노동 조합에 의해 배열. 이진 검색 트리 (BST)에 대한 원래이 작동되는 코드

내가이 작업을 수행 할 수 있도록하려면 :방법, 템플릿으로 구조체를 사용하여 노동 조합을 코드에 내가 포인터의 쌍을 대체 할 조합을 구성하려고

이전에 추가
p = p->pLR.array[value>insertValue]; 

if(value>insertValue) p = p->right; 
else p = p->left; 

1B 분지 . 이것은 값 비싼 분기 오류 예측을 피하기 위해 정확히는 아니지만, 일 수 있습니다. 을 구현하여 이와 같이 [무언가]를 구현할 수 있습니다.

  1. 주로 BinaryNode가 공용체에서 템플리트되고 공용체가 BinaryNode의 요소입니다! 여기

는 내 코드 진행 컴파일되지 않은 :

template <class dataType, class BinaryNode<dataType> > 
union BinaryNodePointerPair { 
struct { 
     BinaryNode<dataType> *left; 
     BinaryNode<dataType> *right; 
    }; 
    BinaryNode<dataType> *array[2]; 
}; 
template <class dataType> 
struct BinaryNode { 
    dataType value; 
    BinaryNodePointerPair<dataType,BinaryNode<dataType> > pLR; 
}; 
[Error] 'BinaryNode' is not a template 

를 리팩토링에서 :

template <class dataType> 
struct BinaryNode { 
    dataType value; 
    BinaryNode *left; 
    BinaryNode *right; 
}; 
  • 내가 시도 무엇 지금까지
      :

    3a. 'X is not a template' error 에 따르면 템플릿은

    template <class dataType, class <dataType>BinaryNode > 
    [Error] expected identifier before '<' token 
    [Error] expected '>' before '<' token 
    [Error] type/value mismatch at argument 2 in template parameter list for 'template<class dataType, int <anonymous> > union BinaryNodePointerPair' 
    

    3B해야한다. 그래서 나는 그것을

    template <class dataType, class BinaryNode<dataType> > 
    

    으로 바꾼다. 그러나 이것은 오류와 원본을 더한다.

    [Error] 'BinaryNode' is not a class template 
    [Error] 'class BinaryNode' is not a valid type for a template non-type parameter 
    [Error] 'BinaryNode' is not a template 
    

    3c. 그래서 나는 다시

    template <class dataType, class BinaryNode<dataType> > 
    

    로 변경하지만이 훨씬 더 오류

    [Error] 'BinaryNode' is not a class template 
    [Error] 'class BinaryNode' is not a valid type for a template non-type parameter 
    [Error] 'BinaryNode' is not a template 
    [Error] 'BinaryNode' is not a template type 
    

    차원을 제공합니다. 이제,이 CRTP 에 섬뜩하게 가까운 (나에게이 밤은 실제 CRTP 말할 필요가 없습니다,하지만 호기심입니다)

    template <class dataType> 
    union BinaryNodePointerPair { 
    struct { 
        BinaryNode<dataType> *left; 
        BinaryNode<dataType> *right; 
    }; 
    BinaryNode<dataType> *array[2]; 
    }; 
    template <class dataType, BinaryNodePointerPair<dataType> > 
    struct BinaryNode { 
        dataType value; 
        BinaryNodePointerPair<dataType> pLR; 
    }; 
    [Error] 'BinaryNode' is not a template 
    

    만 1 오류! 와우! 이것은 승자가되어야합니다!

    3e. 이제 나는 CRTP에 그것을 실제로 강제합니다 ... 그것은 더욱 심각합니다.

    template <class dataType, class BinaryNode<dataType> > 
    union BinaryNodePointerPair { 
        struct { 
         BinaryNode<dataType> *left; 
         BinaryNode<dataType> *right; 
        }; 
        BinaryNode<dataType> *array[2]; 
    }; 
    template <class dataType> 
    struct BinaryNode { 
        dataType value; 
        BinaryNodePointerPair<dataType, BinaryNode<dataType>> pLR; 
    }; 
    [Error] 'BinaryNode' is not a class template 
    [Error] 'class BinaryNode' is not a valid type for a template non-type parameter 
    [Error] 'BinaryNode' is not a template 
    

    CRTP 일 필요는 없습니다. 할 방법이 있습니까?

    해당 공용체의 요소가있는 구조체에 포인터를 사용하여 공용체를 작성하는 방법은 무엇입니까?

  • +0

    서두르는 사람에게 : 나는 그 질문에 무엇이 잘못된지 알 수 있습니까? "좋은 질문을하는 법"의 원칙은 무엇입니까? 더 나은 질문을 어떻게 수정해야합니까? – Karl

    +0

    나는 여기에서 좋은 것으로 간주되는 편집 (채팅 및 메타 해설 줄이기)을했기 때문에 나는 downvoted했다. 그리고 당신은 후속 편집에서 그것들을 덮어 썼다. (일반적으로 사람들이 왜 투표하지 않는 이유는 묻지 않습니다. 대부분의 사람들이 투표를하고 계속 전진 할 것이기 때문입니다.) – halfer

    +1

    구조체의 성능 문제를 실제로 관찰 해 보셨습니까? http://softwareengineering.stackexchange.com/questions/75390/why-should-i-care-about-micro-performance-and-efficency – Caleth

    답변

    2

    나는 정말로 질문을 얻지 않지만, 앞으로 선언문 BinaryNode을 잊어 버리셨습니까?

    template <class dataType> 
    struct BinaryNode; 
    
    template <class dataType> 
    union BinaryNodePointerPair 
    { 
        struct 
        { 
        BinaryNode<dataType> *left; 
        BinaryNode<dataType> *right; 
        }; 
        BinaryNode<dataType> *array[2]; 
    }; 
    
    template <class dataType> 
    struct BinaryNode 
    { 
        dataType value; 
        BinaryNodePointerPair<dataType> pLR; 
    }; 
    
    +0

    이것은 실제로 작동합니다! 내가 그렇게 황홀하지 않으면 나는 나 자신을 거의 부끄러워 할 것이다! 답변으로 표시하려면 어떻게합니까? – Karl