jeudi 5 avril 2018

Why does the value returned from front() exist even after pop() from std::queue?

I'm collecting the value returned from queue::front() into a local variable. As per documentation, queue::front() returns the reference. So if I pop it off from the queue, how can the collected value still exist?

int main()
{
    std::string val;
    {
        std::queue<std::string> q;
        q.push("one");
        q.push("two");
        q.push("three");
        val = q.front();
        q.pop();
        q.pop();
        q.pop();
        std::cout << "is queue empty: " << boolalpha << q.empty() << '\n';
    }

    std::cout << "val: " << val << '\n';
}

The output is:

is queue empty: true
val: one

Why does val still has "one" after pop()'ing

Aucun commentaire:

Enregistrer un commentaire