C++ 理解boost::不相交的集合与存储

C++ 理解boost::不相交的集合与存储,c++,c++11,boost,disjoint-sets,union-find,C++,C++11,Boost,Disjoint Sets,Union Find,我试着去理解,但即使是最简单的例子也不起作用,我也不明白为什么 #include <boost/pending/disjoint_sets.hpp> int main(){ boost::disjoint_sets_with_storage<> union_find; union_find.make_set(1); //.. } #包括 int main(){ boost::不相交的集合与存储联合查找; 联合查找、生成集合(1); //.. }

我试着去理解,但即使是最简单的例子也不起作用,我也不明白为什么

#include <boost/pending/disjoint_sets.hpp>

int main(){
    boost::disjoint_sets_with_storage<> union_find;
    union_find.make_set(1);
    //..
}
#包括
int main(){
boost::不相交的集合与存储联合查找;
联合查找、生成集合(1);
//..
}

上面的代码进行编译,但随后会生成segfault。我想使用整数作为元素,但在中没有“Element”模板参数,因此我假设它推断类型。但是,问题是什么?谢谢

我对使用它还没有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);
#包括
#包括
#包括
typedef boost::不相交的集合与存储Uf;
std::向量相同集合;
//用0和n-1之间的数字对填充向量,其中
//n是集合中唯一元素的数量
// ...
int n=…;//n是唯一集合元素的数目
Uf union_find_set(n);//创建编号为0到n-1的结构
//在不同的集合中。->一组用于0,一组用于1。。。
用于(自动相同:相同集合){
并集查找集。并集(相同。第一,相同。第二);
}
//union_find_集合现在包含带有数字的集合
//0到n-1,根据应组合的集合
//在向量相同的集合中给出。
//检查两个元素是否在同一集合中,即0和2:

你能在这里找到你想要的吗?我从来都不喜欢这个实现。对于我所有的需要,我使用我自己的实现,使用列表和无序映射。@Greg谢谢,但不,它不是同一个接口nion\u find\u set。find\u set(…)只接受一个参数并返回参数集。