Compare objects in vectors

S

shoa

Hello

I have a vector A that stores "a" objects and a vector B that stores "b"
objects. Both object "a" and "b" have one similar field "id".
Now I have to create a function to select all objects in Vector B that have
different "id" compare with objects in vector A and then do something with
them.

For example:
Vector A: have 3 objects
a1 (id=10)
a2 (id=13)
a3 (id =14)

Vector B: have 4 objects
b1 (id=10)
b2 (id =12)
b3 (id=14)
b4 (id=16)

My function will first find out object b2 and b4 (in vector B) that are not
in vector A. Then with object b2 and b4, I have to do something (update
these objects)

In my solution, I have to use two loops "for" to find objects (b2 and b4)
then have a third vector to store (object b3 and b4 as above). Then I have
to loop (go through each object) in this third vector to update these
objects.


However, if there are many objects in Vector A nd B, it is long time to do.
Do you have a better way, please let me know
Thank you
S.Hoa
 
P

Patricia Shanahan

shoa said:
Hello

I have a vector A that stores "a" objects and a vector B that stores "b"
objects. Both object "a" and "b" have one similar field "id".
Now I have to create a function to select all objects in Vector B that have
different "id" compare with objects in vector A and then do something with
them.

For example:
Vector A: have 3 objects
a1 (id=10)
a2 (id=13)
a3 (id =14)

Vector B: have 4 objects
b1 (id=10)
b2 (id =12)
b3 (id=14)
b4 (id=16)

My function will first find out object b2 and b4 (in vector B) that are not
in vector A. Then with object b2 and b4, I have to do something (update
these objects)

In my solution, I have to use two loops "for" to find objects (b2 and b4)
then have a third vector to store (object b3 and b4 as above). Then I have
to loop (go through each object) in this third vector to update these
objects.


However, if there are many objects in Vector A nd B, it is long time to do.
Do you have a better way, please let me know
Thank you
S.Hoa

1. Scan A, constructing a HashSet that contains all id values from A in
Integer form.

2. Scan B, looking each id up in the HashSet. If its not there, update
the B object.

Patricia
 
S

shoa

Thanks

Patricia Shanahan said:
1. Scan A, constructing a HashSet that contains all id values from A in
Integer form.

2. Scan B, looking each id up in the HashSet. If its not there, update
the B object.

Patricia
 

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,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top