erase-remove usage

Discussion in 'C++' started by Wing, Nov 4, 2006.

  1. Wing

    Wing Guest

    Hello,

    I have the following code:

    ////////////////////////////

    class Obj
    {
    public:
    int time;
    string str;
    };

    vector<Obj> array;

    ////////////////////////

    I would like to remove all the "Obj" objects where their "time"
    variables are less than or equal to, say x. Could anyone teach me how
    to use erase-remove to do that?

    Thank you so much!
    Wing, Nov 4, 2006
    #1
    1. Advertising

  2. Wing

    Ian Collins Guest

    Wing wrote:
    > Hello,
    >
    > I have the following code:
    >
    > ////////////////////////////
    >
    > class Obj
    > {
    > public:
    > int time;
    > string str;
    > };
    >
    > vector<Obj> array;
    >
    > ////////////////////////
    >
    > I would like to remove all the "Obj" objects where their "time"
    > variables are less than or equal to, say x. Could anyone teach me how
    > to use erase-remove to do that?
    >

    Read up on std::vector.remove_if(). a google for "vector remove_if"
    will find you some examples.

    --
    Ian Collins.
    Ian Collins, Nov 4, 2006
    #2
    1. Advertising

  3. Wing

    Wing Guest

    Hello,

    I have googled for this topic. However, all the examples I saw are
    based on vector<int>.

    I think that I need to add some extra functions into my code in order
    to do what I want. Anyone can give me some pointer? I am very
    confused.... :(

    Thank you.


    Ian Collins wrote:
    > Wing wrote:
    > > Hello,
    > >
    > > I have the following code:
    > >
    > > ////////////////////////////
    > >
    > > class Obj
    > > {
    > > public:
    > > int time;
    > > string str;
    > > };
    > >
    > > vector<Obj> array;
    > >
    > > ////////////////////////
    > >
    > > I would like to remove all the "Obj" objects where their "time"
    > > variables are less than or equal to, say x. Could anyone teach me how
    > > to use erase-remove to do that?
    > >

    > Read up on std::vector.remove_if(). a google for "vector remove_if"
    > will find you some examples.
    >
    > --
    > Ian Collins.
    Wing, Nov 4, 2006
    #3
  4. Wing

    Ian Collins Guest

    Wing wrote:

    Please don't top post.

    > Ian Collins wrote:
    >
    >>Wing wrote:
    >>
    >>>Hello,
    >>>
    >>>I have the following code:
    >>>
    >>>////////////////////////////
    >>>
    >>>class Obj
    >>>{
    >>>public:
    >>> int time;
    >>> string str;
    >>>};
    >>>
    >>>vector<Obj> array;
    >>>
    >>>////////////////////////
    >>>
    >>>I would like to remove all the "Obj" objects where their "time"
    >>>variables are less than or equal to, say x. Could anyone teach me how
    >>>to use erase-remove to do that?
    >>>

    >>
    >>Read up on std::vector.remove_if(). a google for "vector remove_if"
    >>will find you some examples.
    >>

    > Hello,
    >
    > I have googled for this topic. However, all the examples I saw are
    > based on vector<int>.
    >

    Probably because it's the most straightforward case!

    > I think that I need to add some extra functions into my code in order
    > to do what I want. Anyone can give me some pointer? I am very
    > confused.... :(
    >


    Extending your Obj to give an example:

    #include <vector>
    #include <functional>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <iterator>

    struct Obj
    {
    int time;
    std::string str;
    Obj( int time ) : time(time) {}
    friend std::eek:stream& operator<<( std::eek:stream& out, const Obj& o ){
    return out << o.time;
    }
    };

    struct RemoveAfter : std::unary_function<Obj, bool>
    {
    bool operator()(const Obj& x){ return x.time > 10; }
    };

    int main() {
    std::vector<Obj> array;

    array.push_back( Obj( 5 ) );
    array.push_back( Obj( 15 ) );
    array.push_back( Obj( 10 ) );

    std::copy(array.begin(),
    array.end(),
    std::eek:stream_iterator<Obj>(std::cout," "));
    std::cout << std::endl;

    std::vector<Obj>::iterator last = std::remove_if( array.begin(),
    array.end(),
    RemoveAfter() );
    std::copy(array.begin(),
    last,
    std::eek:stream_iterator<Obj>(std::cout," "));
    std::cout << std::endl;
    }

    --
    Ian Collins.
    Ian Collins, Nov 4, 2006
    #4
    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. metfan
    Replies:
    2
    Views:
    4,849
    Robert Olofsson
    Oct 21, 2003
  2. Nan Li
    Replies:
    2
    Views:
    1,066
    Andrew Koenig
    Nov 8, 2005
  3. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    363
    Pete Becker
    Mar 30, 2006
  4. Replies:
    3
    Views:
    333
    terminator
    Mar 25, 2007
  5. Öö Tiib
    Replies:
    0
    Views:
    734
    Öö Tiib
    Jun 16, 2010
Loading...

Share This Page