So I have this algorithm that needs to find the intersection of three vectors of pointers to objects. The objects are held in another vector.
Right now I sort the vectors by an ID field i the objects, and then run binary searchs, again, 0n this ID field.
I was thinking that I could just as well sort & search the vectors of pointers by the address of the original object rather then dereferencing the object and calling the ID function. Then I thought about it some more, and wondered if the addresses of the memory allocated (using new) for the object vector is guarenteed to be in ascending order already? Because of the way the vectors of pointers are formed, they would also be sorted if this is true, and I could skip the sort step!
I wrote a small chunk of code below to test it, and it seems to be true, but I don't know if this is a general result. I highly doubt it - and doubt I would rely on it, but thought I'd ask.
Right now I sort the vectors by an ID field i the objects, and then run binary searchs, again, 0n this ID field.
I was thinking that I could just as well sort & search the vectors of pointers by the address of the original object rather then dereferencing the object and calling the ID function. Then I thought about it some more, and wondered if the addresses of the memory allocated (using new) for the object vector is guarenteed to be in ascending order already? Because of the way the vectors of pointers are formed, they would also be sorted if this is true, and I could skip the sort step!
I wrote a small chunk of code below to test it, and it seems to be true, but I don't know if this is a general result. I highly doubt it - and doubt I would rely on it, but thought I'd ask.