deque::operator[]

Returns a reference to the deque element at a specified position.

reference operator[]( 
   size_type _Pos 
); 
const_reference operator[]( 
   size_type _Pos 
) const;

_Pos

The position of the deque element to be referenced.

A reference to the element whose position is specified in the argument. If the position specified is greater than the size of the deque, the result is undefined.

If the return value of operator[] is assigned to a const_reference, the deque object cannot be modified. If the return value of operator[] is assigned to a reference, the deque object can be modified.

When compiling with _SECURE_SCL 1, a runtime error will occur if you attempt to access an element outside the bounds of the deque. See Checked Iterators for more information.

// deque_op_ref.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( ) 
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 10 );
   c1.push_back( 20 );
   cout << "The first integer of c1 is " << c1[0] << endl;
   int& i = c1[1];
   cout << "The second integer of c1 is " << i << endl;
   
}
The first integer of c1 is 10
The second integer of c1 is 20

Header: <deque>

Namespace: std

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft