2016-07-11 6 views
1

나는 boost::disjoint_sets_with_storage을 이해하려고 노력하지만 가능한 가장 단순한 예제조차도 작동하지 않으며 그 이유를 이해할 수 없습니다.이해 boost :: disjoint_sets_with_storage

#include <boost/pending/disjoint_sets.hpp> 

int main(){ 
    boost::disjoint_sets_with_storage<> union_find; 
    union_find.make_set(1); 
    //.. 
} 

위의 코드는 컴파일되지만 segfault가 생성됩니다. 정수를 요소로 사용하고 싶지만 boost documentation에는 "요소"템플릿 매개 변수가 없으므로 형식을 유추한다고 가정합니다. 그러나 문제는 무엇입니까? 감사합니다.

+0

당신은 [여기 찾고있는 것을 찾을 수 있습니다 http://stackoverflow.com/questions/4134703/understanding-boostdisjoint-sets](http://stackoverflow.com/questions/4134703/understanding-boostdisjoint -sets) – Greg

+0

나는이 구현을 결코 좋아하지 않았다. 내 모든 필요에 따라 목록 및 정렬되지 않은 맵을 사용하여 자체 구현을 사용합니다. – Arunmu

+0

@ 그렉 감사하지만 아니, 아니 같은 인터페이스 –

답변

0

아직 작업에 100 % 확신하지는 않지만 여기서는 단순화하려고하는 예제가 있습니다. 같은 세트에 있거나 0이 아닌 0에서 n-1까지의 정수로 간단하게 작업 할 수 있다면 가장 쉽다고 생각합니다.

#include <iostream> 
#include <boost/pending/disjoint_sets.hpp> 
#include <vector> 
typedef boost::disjoint_sets_with_storage<> Uf; 

std::vector<pair<int,int>> same_set; 
// fill the vector with number pairs between 0 and n-1 where 
// n is the number of unique elements in the set 
// ... 
int n = ...; // n is the number of unique set elements 

Uf union_find_set(n); // creates the structure with numbers 0 to n-1 
// in seperate sets. -> one set is for 0, one set for 1, ... 

for (auto same : same_set) { 
    union_find_set.union_set(same.first, same.second); 
} 
// union_find_set now contains sets with the numbers 
// 0 to n-1 according to which sets should be combined 
// given in the vector same_set. 

// check whether two elements are in the same set, i.e. 0 and 2: 
std::cout << union_find_set.find_set(0, 2);