The fastest way to find union of sets

后端 未结 7 558
说谎
说谎 2021-01-11 09:39

I have sets of pairs of int like set > x1, x2, ... xn ( n can be between 2 and 20). What is the fastest way to find union of those se

7条回答
  •  执笔经年
    2021-01-11 10:27

    Unfortunately, I believe that you are limited to a linear O(N) solution, as all a union would be is a combination of the elements in both sets.

    template
    S union_sets(const S& s1, const S& s2)
    {
         S result = s1;
    
         result.insert(s2.cbegin(), s2.cend());
    
         return result;
    }
    

提交回复
热议问题