queue::front
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

queue::front 

Returns a reference to the first element at the front of the queue.


value_type& front( ); 
const value_type& front( ) const;

The first element of the queue. If the queue is empty, the return value is undefined.

If the return value of front is assigned to a const_reference, the queue object cannot be modified. If the return value of front is assigned to a reference, the queue object can be modified.

The member function returns a reference to the first element of the controlled sequence, which must be nonempty.

When compiling with _SECURE_SCL 1, a runtime error will occur if you attempt to access an element in an empty queue. See Checked Iterators for more information.

// queue_front.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main() {
   using namespace std;
   queue <int> q1;

   q1.push( 10 );
   q1.push( 20 );
   q1.push( 30 );

   queue <int>::size_type i;
   i = q1.size( );
   cout << "The queue length is " << i << "." << endl;

   int& ii = q1.back( );
   int& iii = q1.front( );

   cout << "The integer at the back of queue q1 is " << ii 
        << "." << endl;
   cout << "The integer at the front of queue q1 is " << iii 
        << "." << endl;
}

The queue length is 3.
The integer at the back of queue q1 is 30.
The integer at the front of queue q1 is 10.

Header: <queue>

Show:
© 2016 Microsoft