What is wrong with this code?

B

BCC

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;
}
}
}
 
D

deancoo

BCC said:
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
 
O

Old Wolf

BCC said:
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 ?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top