c++ - How to remove element from list with a corresponding priority queue? -
i have std::list of objects, rabbits. each rabbit has 2 properties: id , weight. , in list rabbits in order of id. utilize std::priority_queue store pointers rabbit list, in order of weight.
now i'm going utilize priority_queue delete lightest n rabbits in both priority_queue , original list. question is: how delete in original list? sample code:
#include <queue> using namespace std; list<rabbit> rabbitarmy; priority_queue<rabbit, vector<rabbit*>, compareweight> rabbitsortbyweight; (int = 0; < 999; i++) { ..... // each rabbit has different id , weight, codes omitted rabbit rabbit(randomid, randomweight); rabbitarmy.push_back(rabbit); rabbitsortbyweight.push(&rabbitarmy.back()); } // i'll delete n lightest rabbits in priority_queue (int = 0; < n; i++) rabbitsortbyweight.pop(); what original list?
by way, if have list, want set in priority_queue, there improve way pushing elements 1 after another?
so here why arch's code not quite working, , figure improve show op. missing link equality operator ==() removal std::list<>. without std::list<t>::remove() has no way compare whether object sent 1 beingness examined removal.
#include <iostream> #include <iterator> #include <list> #include <vector> #include <queue> #include <iomanip> #include <ctime> using namespace std; // rabbit (i don't have yours). struct rabbit { rabbit(int weight=0, int size=0) : weight(weight), size(size) {}; int weight; int size; // needed std::list<>::remove() bool operator ==(const rabbit& other) { homecoming weight == other.weight && size == other.size; } }; // write output stream std::ostream& operator <<(std::ostream& os, const rabbit& rabbit) { os << '[' << setw(2) << rabbit.weight << ',' << setw(2) << rabbit.size << ']'; homecoming os; } // functor comparing 2 rabbits address struct comparerabbitptrs { bool operator ()(const rabbit* left, const rabbit* right) { homecoming right->weight < left->weight || (right->weight == left->weight && right->size < left->size); } }; // typedefs create life little easier. first list typedef std::list<rabbit> rabbitlist; // priority_queue typedef std::priority_queue<rabbit*, std::vector<rabbit*>, comparerabbitptrs> rabbitqueue; int main() { // seed rng std::srand((unsigned)time(0)); rabbitlist rabbits; rabbitqueue rq; // load rabbits. (int i=1;i<12;++i) { rabbits.push_back(rabbit(std::rand() % 10 + 3,std::rand() % 20 + 5)); rq.push(&rabbits.back()); } // show rabbits std::copy(rabbits.begin(), rabbits.end(), ostream_iterator<rabbit>(cout,"\n")); cout << endl; // remove top n rabbits, in case 2 (int i=0;i<2;++i) { rabbits.remove(*rq.top()); rq.pop(); } // show rabbits again. std::copy(rabbits.begin(), rabbits.end(), ostream_iterator<rabbit>(cout,"\n")); homecoming 0; } sample run output
[11,17] [ 6,17] [ 8,11] [12,14] [ 7, 8] [ 6,19] [11,16] [10,19] [ 6,21] [10,14] [ 7,13] [11,17] [ 8,11] [12,14] [ 7, 8] [11,16] [10,19] [ 6,21] [10,14] [ 7,13] c++ list priority-queue
No comments:
Post a Comment