std::sort

#include
#include
#include


struct PathTrie{
        int score;
        char character;
        int id;
}*aa,*bb,*cc,*dd;


bool prefix_compare(const PathTrie *x, const PathTrie *y) {
  if (x->score == y->score) {
    if (x->character == y->character) {
      return false;
    } else {
      return (x->character > y->character);
    }
  } else {
    return x->score > y->score;
  }
}

int main(){

    struct PathTrie a={1,1,1};
    struct PathTrie d={4,4,2};
    struct PathTrie c={3,3,3};
    struct PathTrie b={4,2,4};
    std::vector prefixes;

    aa=&a;
    bb=&b;
    cc=&c;
    dd=&d;
    prefixes.push_back(aa);
    prefixes.push_back(dd);
    prefixes.push_back(cc);
    prefixes.push_back(bb);
    std::cout << prefixes[0]->score << " " << prefixes[0]->character<< " " << prefixes[0]->id << std::endl;
    std::cout << prefixes[1]->score << " " << prefixes[1]->character<< " " << prefixes[1]->id << std::endl;
    std::cout << prefixes[2]->score << " " << prefixes[2]->character<< " " << prefixes[2]->id << std::endl;
    std::cout << prefixes[3]->score << " " << prefixes[3]->character<< " " << prefixes[3]->id << std::endl;

    std::sort(prefixes.begin(),prefixes.begin()+4,prefix_compare);

    std::cout << prefixes[0]->score << " " << prefixes[0]->character<< " " << prefixes[0]->id << std::endl;
    std::cout << prefixes[1]->score << " " << prefixes[1]->character<< " " << prefixes[1]->id << std::endl;
    std::cout << prefixes[2]->score << " " << prefixes[2]->character<< " " << prefixes[2]->id << std::endl;
    std::cout << prefixes[3]->score << " " << prefixes[3]->character<< " " << prefixes[3]->id << std::endl;

    return 0;
}

你可能感兴趣的:(C/C++)