vector of class

Discussion in 'C++' started by xyz, Jun 30, 2008.

  1. xyz

    xyz Guest

    i have a vector of certain class which has certain parameters
    xxx is my class
    and here is my vector
    std::vector<xxx > yyy;

    in my vector i have the data as below:
    12 abcde 34567 asdf 1
    13 fjggkf 2343 fkjhk 3
    12 fgfgfh 33434 fgh 2
    34 dgdg 5454 fgfdg 2
    .....
    now i want to iterate through my vector inorder i have to delete 2nd
    line in my vector
    here is my iterator which goes through all elements of my vector

    std::vector<xxx >::iterator iter;

    i could able to do with the integer vector but i have problem with the
    vector of certain class
    thank you for any help
     
    xyz, Jun 30, 2008
    #1
    1. Advertising

  2. xyz

    Daniel T. Guest

    On Jun 30, 12:34 pm, xyz <> wrote:

    > i have a vector of certain class which has certain parameters
    > xxx is my class
    > and here is my vector
    > std::vector<xxx > yyy;
    >
    > in my vector i have the data as below:
    > 12 abcde 34567 asdf 1
    > 13  fjggkf 2343 fkjhk 3
    > 12 fgfgfh 33434 fgh 2
    > 34 dgdg 5454 fgfdg 2
    > ....
    > now  i want to iterate through my vector inorder i have to delete 2nd
    > line in my vector
    > here is my iterator which goes through all elements of my vector
    >
    > std::vector<xxx >::iterator iter;
    >
    > i could able to do with the integer vector but i have problem with the
    > vector of certain class


    You do it the same way.

    > thank you for any help


    Use the erase-remove idiom. If your class defines an operator== that
    can compare two of them for equivelance, then simply:

    yyy.erase(remove(yyy.begin(), yyy.end()), yyy.end());

    If your class doesn't have an op==, then you will have to define a
    predicate that returns true for the value(s) that needs to be removed
    and use the remove_if algorithm:

    bool mypred(const xxx& left, const xxx& right);
    // returns true is left is like right.

    yyy.erase(remove_if(yyy.begin(), yyy.end(), &mypred), yyy.end());

    You could also use a function object (functor) for "mypred", possibly
    even compose one from the standard ones that exist or using something
    like the boost lambda library.
     
    Daniel T., Jun 30, 2008
    #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. ding feng
    Replies:
    8
    Views:
    856
    Dhruv
    Jul 2, 2003
  2. pmatos
    Replies:
    6
    Views:
    23,855
  3. Replies:
    8
    Views:
    1,940
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    574
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    369
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page