What is wrong with this code?

Discussion in 'C++' started by BCC, Mar 8, 2005.

  1. BCC

    BCC Guest

    I have two vectors of objects, and these objects contain a double,
    'ref_mass'. I want to remove the elements in one list from the other. My
    erase seems to work okay, except that after erasing the objects
    (successfully), my 'clean' vector is still the same length as the original.
    And if the vector had a length of 100, and I erase 10, the last 10 elements
    (91-100) are just copies of the last real element.

    What am I doing wrong?
    Thanks,
    Bryan

    if (remove_list.size() > 0) {
    // Copy the list
    clean_list = orig_list;
    for (ref_it = clean_list.begin(); ref_it != clean_list.end();) {
    bool b_erase = false;
    for (cmp_it = remove_list.begin(); cmp_it != remove_list.end();
    ++cmp_it) {
    if ((*ref_it).ref_mass == (*cmp_it).ref_mass) {
    b_erase = true;
    break;
    }
    }
    if (b_erase) {
    ref_it = clean_list.erase(ref_it);
    }
    else {
    ++ref_it;
    }
    }
    }
    BCC, Mar 8, 2005
    #1
    1. Advertising

  2. BCC

    deancoo Guest

    "BCC" <> wrote in message
    news:ea6Xd.5015$...
    >I have two vectors of objects, and these objects contain a double,
    >'ref_mass'. I want to remove the elements in one list from the other. My
    >erase seems to work okay, except that after erasing the objects
    >(successfully), my 'clean' vector is still the same length as the original.
    >And if the vector had a length of 100, and I erase 10, the last 10 elements
    >(91-100) are just copies of the last real element.
    >
    > What am I doing wrong?
    > Thanks,
    > Bryan
    >
    > if (remove_list.size() > 0) {
    > // Copy the list
    > clean_list = orig_list;
    > for (ref_it = clean_list.begin(); ref_it != clean_list.end();) {
    > bool b_erase = false;
    > for (cmp_it = remove_list.begin(); cmp_it != remove_list.end();
    > ++cmp_it) {
    > if ((*ref_it).ref_mass == (*cmp_it).ref_mass) {
    > b_erase = true;
    > break;
    > }
    > }
    > if (b_erase) {
    > ref_it = clean_list.erase(ref_it);
    > }
    > else {
    > ++ref_it;
    > }
    > }
    > }
    >


    Have you thought about just using "set_difference". I think that might be a
    lot simpler. Of course, you'd have to overload the LessThan operator.

    d
    deancoo, Mar 8, 2005
    #2
    1. Advertising

  3. BCC

    Old Wolf Guest

    BCC wrote:
    > I have two vectors of objects. I want to remove the elements in
    > one list from the other. My erase seems to work okay, except
    > that after erasing the objects (successfully), my 'clean' vector
    > is still the same length as the original.
    >
    > And if the vector had a length of 100, and I erase 10, the
    > last 10 elements (91-100) are just copies of the last real element.
    >
    > What am I doing wrong?
    >
    > if (remove_list.size() > 0) {
    > // Copy the list
    > clean_list = orig_list;
    > for (ref_it = clean_list.begin(); ref_it != clean_list.end();) {
    > bool b_erase = false;
    > for (cmp_it = remove_list.begin(); cmp_it != remove_list.end();
    > ++cmp_it) {
    > if ((*ref_it).ref_mass == (*cmp_it).ref_mass) {
    > b_erase = true;
    > break;
    > }
    > }
    > if (b_erase) {
    > ref_it = clean_list.erase(ref_it);
    > }
    > else {
    > ++ref_it;
    > }
    > }
    > }


    Your code works fine on my machine. Perhaps you are making
    a silly error, such as checking clean_list.capacity() instead
    of clean_list.size() afterwards ?
    Old Wolf, Mar 8, 2005
    #3
    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. walala
    Replies:
    3
    Views:
    2,166
    Ralf Hildebrandt
    Sep 10, 2003
  2. willem oosthuizen

    What is wrong with the following code?

    willem oosthuizen, Oct 10, 2003, in forum: VHDL
    Replies:
    9
    Views:
    1,247
  3. Matthew
    Replies:
    7
    Views:
    644
    Priscilla Walmsley
    Jan 7, 2005
  4. David. E. Goble
    Replies:
    9
    Views:
    453
    David. E. Goble
    Feb 2, 2005
  5. kiran
    Replies:
    12
    Views:
    1,079
    Scott Sauyet
    Dec 7, 2011
Loading...

Share This Page