elegant way to remove all elements of a vector that are contained in another vector?

后端 未结 5 766
Happy的楠姐
Happy的楠姐 2021-02-14 00:08

While looking over some code I found loopy and algorithmically slow implementation of std::set_difference :

 for(int i = 0; i < a.size(); i++)
 {
  iter = std         


        
5条回答
  •  我在风中等你
    2021-02-14 00:23

    This one does it in O(N+M), assuming both arrays are sorted.

      auto ib = std::begin(two);
      auto iter = std::remove_if (
           std::begin(one), std::end(one),
           [&ib](int x) -> bool {
                           while  (ib != std::end(two) && *ib < x) ++ib;
                           return (ib != std::end(two) && *ib == x);
                         });
    

提交回复
热议问题