Why can't I make a priority queue of type struct objects?

Viliami Mahe

So I kinda know that I can't make a priority queue with type struct but I don't exactly understand the reason why? I mean if you can create template classes to make your own type, why would a struct be any different?

This is what my code looks like in main.cpp:

#include <iostream>
#include <queue>
#include <string>

struct DATA {
    std::string key;
    int data;
};

int main() {
    std::priority_queue<DATA> priorityQ;
    DATA newItem;
    newItem.key = "apples";
    newItem.data = 3;
    priorityQ.push(newItem);
    std::cout << priorityQ.top().key << std::endl;

    std::cout << "Press the 'ENTER' key to continue...";
    std::cin.get();

    return 0;
}

The error come up with:

Error   C2678   binary '<': no operator found which takes a left-hand
operand of type 'const DATA' (or there is no acceptable conversion)
TestProj    c:\program files (x86)\microsoft visual studio 
14.0\vc\include\xstddef    Line: 240    

I tried making this operator overload:

bool operator<(const DATA& a, const DATA& b) {
    return a.data > b.data;
}

But it still does not compile...

My question is: is it possible to put struct objects in priority queues and , and if not, why?

xaxxon

Here you go.

#include <iostream>
#include <queue>
#include <string>

struct DATA {
    std::string key;
    int data;
    // EITHER THIS
    bool operator<(const DATA & d2) const {return <your logic here>;}

};
// OR THIS
bool operator<(const DATA &d1, const DATA & d2){return <your logic here>;}


int main() {
    std::priority_queue<DATA> priorityQ;
    DATA newItem;
    newItem.key = "apples";
    newItem.data = 3;
    priorityQ.push(newItem);
    std::cout << priorityQ.top().key << std::endl;

    std::cout << "Press the 'ENTER' key to continue...";
    std::cin.get();

    return 0;
}

live: https://godbolt.org/g/uC6JdB

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I make a priority queue of type struct objects?

From Dev

In golang why I can't use the struct as nested struct type?

From Dev

Why can't I use 'Type' as the name of an enum embedded in a struct?

From Dev

Making Priority Queue of Objects

From Dev

compare objects in priority queue

From Dev

Why can't I pass methods as objects?

From Dev

Why can't I change objects in a vector?

From Dev

Why can't I dereference pointer to objects?

From Dev

Why can't I pass methods as objects?

From Dev

Why can't I have pointers to objects, which have the same type of pointer as a member variables?

From Dev

Why I can't make Google Chrome open "mailto" content type?

From Dev

How can I consume data type of Rc<RefCell<T>> in struct?

From Dev

Why can't I complete the type with the typedef?

From Dev

Why can't I cast `this` to a generic type?

From Dev

Why priority_queue doesn't have front() but top()

From Dev

Why priority_queue doesn't have front() but top()

From Java

Why can't I store a value and a reference to that value in the same struct?

From Dev

Why I can't change char array attribute of a struct?

From Dev

Why can't I assign a struct variable using curly brackets {}?

From Dev

Why can't i dereference a pointer to struct in C?

From Dev

Why can't I assign an int to the union member of a struct member?

From Dev

How can I make an IObservable from a queue, so that the sequence doesn't end when the queue is empty?

From Dev

Queue for storing struct objects

From Dev

Why can't I type a g̃ the same way I type ñ?

From Dev

Why can't I type a g̃ the same way I type ñ?

From Dev

Can I std::unique a std::priority_queue

From Dev

how can I specify the comparator class of priority_queue at runtime

From Dev

Can I use member function of underlying container of priority_queue

From Dev

I can't understand why can't Haskell deduce this type

Related Related

  1. 1

    Why can't I make a priority queue of type struct objects?

  2. 2

    In golang why I can't use the struct as nested struct type?

  3. 3

    Why can't I use 'Type' as the name of an enum embedded in a struct?

  4. 4

    Making Priority Queue of Objects

  5. 5

    compare objects in priority queue

  6. 6

    Why can't I pass methods as objects?

  7. 7

    Why can't I change objects in a vector?

  8. 8

    Why can't I dereference pointer to objects?

  9. 9

    Why can't I pass methods as objects?

  10. 10

    Why can't I have pointers to objects, which have the same type of pointer as a member variables?

  11. 11

    Why I can't make Google Chrome open "mailto" content type?

  12. 12

    How can I consume data type of Rc<RefCell<T>> in struct?

  13. 13

    Why can't I complete the type with the typedef?

  14. 14

    Why can't I cast `this` to a generic type?

  15. 15

    Why priority_queue doesn't have front() but top()

  16. 16

    Why priority_queue doesn't have front() but top()

  17. 17

    Why can't I store a value and a reference to that value in the same struct?

  18. 18

    Why I can't change char array attribute of a struct?

  19. 19

    Why can't I assign a struct variable using curly brackets {}?

  20. 20

    Why can't i dereference a pointer to struct in C?

  21. 21

    Why can't I assign an int to the union member of a struct member?

  22. 22

    How can I make an IObservable from a queue, so that the sequence doesn't end when the queue is empty?

  23. 23

    Queue for storing struct objects

  24. 24

    Why can't I type a g̃ the same way I type ñ?

  25. 25

    Why can't I type a g̃ the same way I type ñ?

  26. 26

    Can I std::unique a std::priority_queue

  27. 27

    how can I specify the comparator class of priority_queue at runtime

  28. 28

    Can I use member function of underlying container of priority_queue

  29. 29

    I can't understand why can't Haskell deduce this type

HotTag

Archive