2016-07-11 2 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

@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); 
Смежные вопросы