queue.back()指向最新插入queue中的值,而非队尾元素,
如:queue.pop()多次,并不会影响queue.back()的值。
STL 英文back()解释:
reference& back();
const_reference& back() const;
Access last element
Returns a reference to the last element in the queue. This is the "newest" element in the queue (i.e. the last element pushed into the queue).
This member function effectively calls member back of the underlying container object.
queue.front()指向最旧插入queue的元素值。
STL 英文front()解释:
reference& front(); const_reference& front() const;
Access next element
Returns a reference to the next element in the queue.
The next element is the "oldest" element in the queue and the same element that is popped out from the queue when queue::pop is called.
This member function effectively calls member front of the underlying container object.
判断队列为空使用empty或者front,不能使用back查看。
// queue::front
#include <iostream> // std::cout
#include <queue> // std::queueint main ()
{std::queue<int> myqueue;myqueue.push(77);myqueue.push(16);std::cout<<myqueue.back()<<std::endl;std::cout<<myqueue.front()<<std::endl;myqueue.pop();std::cout<<myqueue.back()<<std::endl;std::cout<<myqueue.front()<<std::endl;myqueue.pop();std::cout<<myqueue.back()<<std::endl;std::cout<<myqueue.front()<<std::endl;std::cout << "myqueue.front() is now " << myqueue.front() << '\n';return 0;
}
queue::front - C++ Reference