2016-07-11 38 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); 
    //.. 
} 

上面的代碼編譯,但然後它會產生段錯誤。我想使用整數作爲元素,但在boost documentation中沒有「元素」模板參數,所以我假設它推斷類型。但是,問題是什麼?謝謝

+0

你會發現你在找什麼在這裏[http://stackoverflow.com/questions/4134703/understanding-boostdisjoint-sets](http://stackoverflow.com/questions/4134703/understanding-boostdisjoint -sets) – Greg

+0

我從來不喜歡這個實現。爲了我所有的需要,我使用我自己的實現使用列表和無序地圖。 – Arunmu

+0

@Greg謝謝但不,它的界面不一樣 –

回答

0

我還沒有100%的信心,但我給了一個例子,我試着簡化這裏。 我認爲這是最簡單的,如果你可以簡單地使用從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);