Assume i have
struct node{
int val;
node* left;
};
Now, I have a priority_queue<node> sth;
What does the following do:
node temp = sth.top();// does it perform copy
sth.pop();
temp.left = sth.top(); // is this allowed?
How do I pop an element from the queue and store it in temp.left?
Yes, this makes a copy of the top element and stores it in
temp.temp.leftpoints to the same place as top node’s left pointer. Note thattop()actually returns a reference but this line asks for a copy.node &temp = sth.top();would declare a reference to the top element.Removes the top node from the queue. You may have just broken your data structure here. Any pointers to that node object (e.g., left pointers of other nodes in the queue) are now invalid.
No, this won’t compile.
temp.leftis a pointer andtop()returns a reference to an object. At a minimum, you need to get the address of that object:temp.left = &sth.top();