how to remove all even integers from set in c++

前端 未结 4 1878
梦谈多话
梦谈多话 2020-12-19 02:37

I\'m new to C++. I\'d like to know how experienced coders do this.

what I have:

set s;
s.insert(1);
s.insert(2);
s.insert(3);
s.insert(4);         


        
4条回答
  •  囚心锁ツ
    2020-12-19 02:50

    The best way is to use the combination of remove_if and erase

    s.erase(remove_if(s.begin(), s.end(), evenOddFunctor), s.end())
    

    This will be helpful http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Erase-Remove

    Also Refer to effective STL by scott meyers

    Edit: Although my solution is wrong i am not deleting it. It might be a good learning for someone like me who does not about mutable/immutable iterators

提交回复
热议问题