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

后端 未结 5 767
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:50

    after thinking for a while I thought of this
    (note:by answering my own Q im not claiming this is a superior to offered A):

    vector a{3,2,7,5,11,13}, b{2,3,13,5};
    set bs(b.begin(), b.end());
    for (const auto& num: bs)
        cout << num << " ";
    cout  << endl;
    for (const auto& num: a)
        bs.erase(num);
    vector result(bs.begin(), bs.end());
    for (const auto& num: result)
        cout << num << " ";
    

提交回复
热议问题