c++queue

empty queue prints for me value


i created an object from struct then pushed into queue then pop it after that i print what inside it printed for me salary how that comes the queue should be empty i also tried to replace .salary with the other variables it gives me their values.

#include <iostream>
#include <queue>
using namespace std;

struct employee {
    string name;
    int age;
    int salary;
};

int main() {

    queue<employee> q;
    employee e {"Rio",22,1000};

    q.push(e);

    q.pop();
    cout<<q.back().salary<<endl;

    return 0;
}

i tried to push an object named employee to queue then i pop it from the queue then i used .back() function and it gives me value while the queue is empty


Solution

  • If the queue is empty, then calling .back() on it has undefined behavior. Any behavior of the program is valid and no specific behavior can be expected.