logo
down
shadow

C++ priority queue in ascending order by specific method for objects


C++ priority queue in ascending order by specific method for objects

By : Mike
Date : November 21 2020, 07:01 PM
Does that help The CPP Reference link to Priority Queue provides that a priority queue can be defined as:
code :
template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class priority_queue;
struct MyClass{
    int count;
    int key;
    int xVal() { return count; };
};
//  Lambda skeleton: [capture preferences](arguments){ body }
auto cmp = [](MyClass left, MyClass right) {return left.xVal() > right.xVal();};
std::priority_queue<MyClass, std::vector<MyClass>, decltype(cmp)> queue(cmp);
struct CmpFunctor{
    bool operator()(MyClass left, MyClass right) const {
        return left.xVal() > right.xVal();
    }
};
auto cmp = CmpFunctor()
std::priority_queue<MyClass, std::vector<MyClass>, decltype(cmp)> queue(cmp);


Share : facebook icon twitter icon
How to preserve the order of elements of the same priority in a priority queue implemented as binary heap?

How to preserve the order of elements of the same priority in a priority queue implemented as binary heap?


By : axaxaxax
Date : March 29 2020, 07:55 AM
I hope this helps you . One solution is to add time of insertion attribute to the inserted element. That may be just a simple counter incremented each time a new element is inserted into the heap. Then when two elements are equal by priority, compare the time of insertion.
Priority Queue for objects that may change: what is the best method for implementing them?

Priority Queue for objects that may change: what is the best method for implementing them?


By : user3394711
Date : March 29 2020, 07:55 AM
it fixes the issue I need to store objects in a heap/priority queue. However, the objects may change so that their ordering changes, so fixUp or fixDown may need to be called on them. These methods are private and Java PriorityQueue wont let me do this. My options are: ,
Seems pretty inefficient, double the work.
How to make a priority queue use a variable from custom a class (Ascending/descending)

How to make a priority queue use a variable from custom a class (Ascending/descending)


By : huoxiaochai
Date : March 29 2020, 07:55 AM
I wish this helpful for you You actually don't need the additional vector which wraps your priority_queue as the priority_queue itself has 2 additional default arguments: (first one is the type, in your case Person*), second one is the container type and the third one is the compare predicate. below you can see using a lambda function as a compare predicate for your priority queue.
code :
#include <vector>
#include <string>
#include <queue>
#include <iostream>

using namespace std;

class Person
{
public:
    string name;
    int height;
    int age;

    Person(string n, int h, int a): name(n), height(h), age(a) {}
};


ostream& operator<<(ostream &cout, const Person* p) {
    return cout << p->name << " height=" << p->height << " age=" << p->age << " ";
}

int main()
{
    auto cmp = [](const Person* pl, const Person* pr) {
        return (pl->age < pr->age);
    };

    priority_queue<Person*, vector<Person*>, decltype(cmp)> persons(cmp);

    persons.push(new Person("a", 100, 10));
    persons.push(new Person("b", 120, 20));
    persons.push(new Person("c", 110, 15));

    while (!persons.empty()) {
        cout << persons.top() << endl;
        persons.pop();
    }

    return 0;
}
C++ priority queue in ascending order by specific method for unique pointers to objects

C++ priority queue in ascending order by specific method for unique pointers to objects


By : user2025300
Date : March 29 2020, 07:55 AM
will help you You got unknown random order because when you have greater the following action is performed
code :
// pseudocode
cmp(T lhs, T rhs) {
  return lhs > rhs;
}
// Psuedocode when greater used:
cmp (unique_ptr<Foo>& lhs, unique_ptr<Foo>& rhs)
{
  lhs.get () > rhs.get()
}
auto cmp = [](const unique_ptr<Foo>& left, const unique_ptr<Foo>& right) 
{                   ^^^^^^^^^^^^^^^^
    return left->yVal() > right->yVal();
};             ^^
priority_queue<unique_ptr<Foo>, vector<unique_ptr<Foo>>, decltype(cmp) > Queue(cmp);
 // comparator as function object with overloaded operator()
 struct Cmp {
   bool operator()(const std::unique_ptr<Foo>& left, const std::unique_ptr<Foo>& right) const {
     return left->xVal() > right->xVal();
   }
 };

class YourClass {
public:
   std::priority_queue<std::unique_ptr<Foo>, std::vector<std::unique_ptr<Foo>>, Cmp> Queue;
Scala ordered priority queue that always has the lowest number as the head, ascending order

Scala ordered priority queue that always has the lowest number as the head, ascending order


By : derf
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further PriorityQueue.apply and PriorityQueue.empty both take an implicit Ordering instance that will be used to order the contents—the head will be the "largest" value according to that ordering. You're getting the default one for tuples, which is a lexicographic ordering on the elements of the tuple, which isn't what you want, since it'll make the tuple with the largest first element the head.
There are a couple of ways you can solve this issue. The easiest is just to call .reverse on your queue, which will give you a new queue with the same contents but the opposite ordering, which means the tuple with the lowest value will be the head.
Related Posts Related Posts :
  • What's wrong with these pointer initialization
  • Access Violation Using memcpy or Assignment to an Array in a Struct
  • Wrong operator() overload called
  • Subtractively sweep a solid through another in python (or C++)
  • boost ublas: rotate 2d vector
  • How can a Windows program temporarily change its time zone?
  • small & readable scheme interpreter in C++?
  • Cannot change the height of a combo box in the VS Dialog Editor
  • Access reading error when using class member variable
  • How to tell if two exe's are the same code-wise?
  • Modifying an old Windows program not to call exit after a keypress without source access
  • What is name lookup mechanism?
  • What does the C++ compiler error "looks like a function definition, but there is no parameter list;" mean?
  • Why does this crash with access violation to 0xcccccc...?
  • How to find out how namespace got polluted?
  • What is the _REENTRANT flag?
  • Managing many objects at once
  • How to create ActiveX DLL in Visual C++
  • Generic allocator class without variadic templates?
  • Comparation in JNI
  • Using a function with reference as a function with pointers?
  • How to initialize an unsigned long long type?
  • How to practically customize IE context menu?
  • Trying to Create a ToolBar with an ImageList, not working
  • How does the destructor know when to activate itself? Can it be relied upon?
  • Conway's Game of Life - C++ and Qt
  • Accessing any structs members at run-time
  • Including #includes in header file vs source file
  • How does switch compile in Visual C++ and how optimized and fast is it?
  • How to override nested C++ objects methods?
  • penalty for "inlined" classes
  • C++ Library for implementing a web services api over legacy code?
  • C++-Singleton class
  • Floating point precision in Visual C++
  • Compiler error when overriding virtual methods
  • What is the point of function pointers?
  • Which is faster in memory, ints or chars? And file-mapping or chunk reading?
  • ISO file in C++
  • Expected Class-name before { token
  • Can I trust floats or doubles representing integers to retain precision?
  • Qt execute multiple get with QNetworkAccessManager
  • Big and Little endian question
  • Visual Studio 2008 compiles anything in C++ file?
  • C++ pointer to functions, Beginner Question
  • Fastest algorithm for primality test
  • Passing array of pointers to another class
  • Handling macro redefinition without modifying .h files ... C / C++ language
  • Implement SSL with SSPI: How to start?
  • Creating a simple VS2008 visualizer inside autoexp.dat (problem with casting)
  • C++ - Unicode Newline
  • Program crashing with 'std::out_of_range' error
  • Use signed or unsigned char in constructing CString?
  • Parallel execution policies in C++ 17
  • C++ Class Templates (Queue of a class)
  • Convert image into useable byte array in C?
  • pointer to preallocated memory as an input parameter and have the function fill it
  • Animated Image in Win32
  • error on compiling statically linked library created in Qt
  • How to easily pass a very long string to a worker process under Windows?
  • Can a class add friend classes at runtime in C++?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk