1

So basically, I have a struct that, along with other members, has x, y and z values to represent a 3D point; I have then a vector of said structs which is built by some functions.

struct myStruct{
    char x;
    char y;
    char z;
    // some other members
};

vector<myStruct> myVector = myVectorBuildingFunction(...);

Now, I would like to sort the structs in the vector by the distance between their 3D point (x, y, z members) and another variable point in the space.. is that possible without rebuilding the structs' members one by one (they're relatively many) or remaking entirely my initial vector building function?

Banderi
  • 656
  • 3
  • 7
  • 29

2 Answers2

2

You can use std::sort with lambdas, like this:

myStruct pointOfInterest = ...; // Set the point of interest
sort(mMyClassVector.begin(), mMyClassVector.end(), 
    [&](const myStruct & lhs, const myStruct & rhs) -> bool
{
    double distanceLhs = computeDistance(pointOfInterest, lhs);
    double distanceRhs = computeDistance(pointOfInterest, rhs);
    return distanceLhs < distanceRhs;
});
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Or can overload the `operator()` in the structure, if this is what you will be taking as a comparision metric for complete course of your program. Just a suggestion. – Aman Deep Gautam Jul 13 '13 at 23:38
  • 1
    @AmanDeepGautam That would be harder, because you would need to store the point of interest inside the comparator structure. Lambdas provide a more concise approach to this problem. – Sergey Kalinichenko Jul 13 '13 at 23:41
  • @dasblinkenlight I am kind of confused here with the statement "distance between their 3D point" in the question. I think everything is in the structure itself is what OP meant. I may be wrong. I know it is intuitive to find distance from some other point(like origin) but I am not sure if it is that metric here. – Aman Deep Gautam Jul 13 '13 at 23:48
  • @AmanDeepGautam My understanding is that `myStruct` has other members in addition to `x`, `y`, and `z`. The first three members represent "their point", the rest of the members are not used for comparison. – Sergey Kalinichenko Jul 13 '13 at 23:50
  • @dasblinkenlight Yup, that's exactly what I meant. Er pardon me, is computeDistance() a function I have to write or is it an actual function..? – Banderi Jul 13 '13 at 23:55
  • @Banderi Yes, that's a function that you need to write. This could be a member function on the `myStruct` as well, and it does not have to take `myStruct` as a second parameter - a 3D point would work as well. – Sergey Kalinichenko Jul 13 '13 at 23:57
  • @dasblinkenlight apparently what you understood was correct. I think sometimes we should just go by intuition..:) – Aman Deep Gautam Jul 14 '13 at 00:09
1

Yes, it's possible using comparator function or functors.

struct byDistTo {
   myStruct point;
   byDistTo(myStruct point): point(point){}
   bool operator() (const& myStruct a, const& myStruct b) const {
     // define getDistance yourself
     return getDistance(a, point) < getDistance(b, point); 
   }
}

And later call std::sort:

vector<myStruct> myVector = myVectorBuildingFunction(...);
myStruct point = {1,2,3}; // define that 'another varialbe`
std::sort(myVector.begin(), myVector.end(), byDistTo(point));
Community
  • 1
  • 1
sasha.sochka
  • 14,395
  • 10
  • 44
  • 68