0

I have two vectors full of structs that are very simple:

typedef struct{
    //three vertex ids
    uint a,b,c;

} Face;

I'm currently trying to run set_intersection like so:

set_intersection(listOfFaces1.begin(),listOfFaces1.end(),listOfFaces2.begin(),listOfFaces2.end(), back_inserter(facesToDelete));

I'm guessing I need to overwrite some comparator somehow? But I'm not sure how to go about defining equality between two Face objects...

Any help would be much appreciated.

  • 2
    Have a look at [this question](http://stackoverflow.com/questions/6955578/subtraction-and-intersection-of-two-vectors-of-pointers-in-c) – Joel Feb 25 '15 at 06:10
  • You need to (a) define `operator<` for the class or (b) define a functor/function that can compare two `Face`s. If you use (b), you have pass the functor/function as the last argument. – R Sahu Feb 25 '15 at 06:24
  • @RSahu I know about passing a function as the last argument, but examples I've found online simply define when one object is < the other, not ==. And in this case there isn't anyway to determine which Face object is < the other. I only want to check equality, but there doesn't seem to be a way to do that with a comparator function that I can pass into set_intersection()? – user2976199 Feb 25 '15 at 06:27
  • 1
    `set_intersection` will use `operator<` to determine equivalency between elements, `!(a < b) && !(b < a)` implies `a` is equivalent to `b`. Your `operator<` implementation seems suspect though, you probably want to do a lexicographical comparison involving all 3 data members, not just `Face::a`. – Praetorian Feb 25 '15 at 06:33

1 Answers1

1

First of all, when you are programming in C++, you can just use:

struct Face {
    uint a,b,c;
};

Here's a simple strategy for implementing operator< that works for the algorithms and containers in the standard library.

struct Face {
    uint a,b,c;

    bool operator<(Face const& rhs) const
    {
       if ( a != rhs.a )
       {
          return ( a < rhs.a);
       }
       if ( b != rhs.b )
       {
          return ( b < rhs.b);
       }
       return ( c < rhs.c);
    }
};

or, as suggested by @Praetorian,

struct Face {
    uint a,b,c;

    bool operator<(Face const& rhs) const
    {
       return std::tie(a, b, c) < std::tie(rhs.a, rhs.b, rhs.c); 
    }
};
R Sahu
  • 204,454
  • 14
  • 159
  • 270