【发布时间】:2018-04-05 07:37:07
【问题描述】:
我正在将从 queue::front() 返回的值收集到一个局部变量中。根据文档, queue::front() 返回引用。 那么如果我从队列中弹出它,收集的值怎么还存在呢?
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';
}
输出是:
is queue empty: true
val: one
为什么 val 在 pop()'ing 之后仍然有 "one"
【问题讨论】: