Udostępnij za pośrednictwem


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;

Parameters

  • _Pos
    The position of the deque element to be referenced.

Return Value

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.

Remarks

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.

Example

// 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

Requirements

Header: <deque>

Namespace: std

See Also

Reference

deque Class

deque::operator[] and deque::at

Standard Template Library

Other Resources

deque Class Members