Sort std :: set using operator () to order inserts

I continue this post after This  we have a class:

class LaneConnector {
public:

    const Lane* getLaneFrom() const {
        return From;
    }
    const Lane* getLaneTo() const {
        return To;
    }

private:

    Lane* From;
    Lane* To;
}

and functor for comparison:

struct MyLaneConectorSorter {
  bool operator() (const LaneConnector* rhs, const LaneConnector* lhs) const
  {
    // you may want to put some null pointer checks in here
    const Lane* a = lhs->getLaneFrom();
    const Lane* b = rhs->getLaneFrom();
    return a->getLaneID() < b->getLaneID();
  }
};

and finally a set of source and destination:

const std::set<LaneConnector*> src = ..... ;

const std::set<LaneConnector*, MyLaneConectorSorter> dest(src.begin(), src.end(), MyLaneConectorSorter());

The size of the installation set will be 1, while src has more (14 in my case)

what could i do wrong? I appreciate your kind comments. Thanks you

0
source share
2 answers

std::set tracks items based on a key . In your comparator you have return a->getLaneID() < b->getLaneID();. Thus, Lane IDimplicitly becomes a key. Because if aand bhave the same LaneID, then both MyLaneConectorSorter(a, b)and MyLaneConectorSorter(b, a)return false.

, set LaneConnectior LaneID.

+1

, . !

, LaneConnectors . , GetLaneFrom() → GetLaneID() LaneConnectors

0

All Articles