c++ test if 2 sets are disjoint

后端 未结 5 1770
一个人的身影
一个人的身影 2020-12-28 09:15

I know the STL has set_difference, but I need to just know if 2 sets are disjoint. I\'ve profiled my code and this is slowing my app down quite a b

5条回答
  •  遥遥无期
    2020-12-28 09:35

    Use std::set_intersection, and see if the output is empty. You could check to see if the range of the two sets (area covered by the begin and end iterators) overlap first, but I suspect that set_intersection might already do this.

    These are all O(n) operations, as is is_disjoint. If O(n) is unacceptable, you'll have to build some side storage to "keep track" of the disjoint-ness of the sets as you add/remove elements. Here, you would pay the overhead at insertion time (by updating your "isdisjoint" structure for each set modification), but is_disjoint could be implemented cheaply. This may or may not be a good tradeoff to make.

提交回复
热议问题