Removal Of Elements in a Vector Conatings User-Defined Object Basedon Value Contained in Object

Discussion in 'C++' started by Adam Hartshorne, Mar 3, 2005.

  1. Hi All,

    I have a vector of MyObject's user defined objects(with each containing
    a bool type variable called myFlag). I want to go through the whole
    vector and delete from it all the MyObjects's that have their myFlag set
    to false. I can't work out a simple way of doing this, any suggests
    would be much appreciated,

    Adam
    Adam Hartshorne, Mar 3, 2005
    #1
    1. Advertising

  2. Re: Removal Of Elements in a Vector Conatings User-Defined Object Based on Value Contained in Object

    "Adam Hartshorne" <> wrote...
    > I have a vector of MyObject's user defined objects(with each containing a
    > bool type variable called myFlag). I want to go through the whole vector
    > and delete from it all the MyObjects's that have their myFlag set to
    > false. I can't work out a simple way of doing this, any suggests would be
    > much appreciated,


    Create a custom functor

    class MyFlagTester {
    bool desired;
    public:
    MyFlagTester(bool d) : desired(d) {}
    bool operator()(MyObject const& o) const
    { return o.myFlag == desired; }
    };

    Then use it:

    myVector.erase(std::remove_if(myVector.begin(), myVector.end(),
    MyFlagTester(false)));

    V
    Victor Bazarov, Mar 3, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Stranger
    Replies:
    1
    Views:
    859
    Natty Gur
    Aug 24, 2003
  2. Oodini
    Replies:
    1
    Views:
    1,735
    Keith Thompson
    Sep 27, 2005
  3. Replies:
    8
    Views:
    1,877
    Csaba
    Feb 18, 2006
  4. senthil
    Replies:
    3
    Views:
    95
    Augie De Blieck Jr.
    Mar 26, 2007
  5. Pieter Van Waeyenberge
    Replies:
    2
    Views:
    135
    Pieter Van Waeyenberge
    Feb 16, 2004
Loading...

Share This Page