从代码中可以看到,我想两次重载

struct AttSet{
int nodeID;
double dist;

bool operator < (const AttSet & str) const{
    return (dist < str.dist);
}
/*
bool operator <(const AttSet & str){
    return (nodeID < str.nodeID);
*/
bool operator == (const AttSet & str){
    return nodeID == str.nodeID;
}};

void compLoc(Edge *edge, vector<Node*> &vertices){
int l = edge->length;
int vl = edge->head->nodeID;
int vr = edge->tail->nodeID;
/*
sort(vertices[vl]->attSet.begin(), vertices[vl]->attSet.end());
sort(vertices[vr]->attSet.begin(), vertices[vr]->attSet.end());
vector<AttSet> vInterSec;
set_intersection(vertices[vl]->attSet.begin(), vertices[vl]->attSet.end(), vertices[vr]->attSet.begin(), vertices[vr]->attSet.end(), back_inserter(vInterSec));
*/}

最佳答案

我认为您可以通过使用functor并在AttSet之外获取比较器(operator 这是一个简单的示例:

struct AtrComparator {
     bool distcmp;
     AttrComparator(bool distcmp): distcmp(distcmp) {}
     bool operator() (const AttSet &s1, const AttSet &s2) {
          if(distcmp) {
               return s1.dist < s2.dist;
          } else {
               return s1.nodeID < s2.nodeID;
          }
     }
}


然后,您可以通过不同的feed,dist或nodeID进行排序。
。例如:

sort(vertices[vl]->attSet.begin(), vertices[vl]->attSet.end(), AttComparator(true));
sort(vertices[vl]->attSet.begin(), vertices[vl]->attSet.end(), AttComparator(false));

09-11 17:32