Translations of this page?:

at

Syntax:

    #include <deque>
    TYPE& at( size_type loc );
    const TYPE& at( size_type loc ) const;

The at() function returns a reference to the element in the deque at index loc. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the deque.

For example, consider the following code:

   deque<int> v( 5, 1 );
   for( int i = 0; i < 10; i++ ) {
     cout << "Element " << i << " is " << dq[i] << endl;
   }

This code overrunns the end of the deque, producing potentially dangerous results. The following code would be much safer:

   deque<int> v( 5, 1 );
   for( int i = 0; i < 10; i++ ) {
     cout << "Element " << i << " is " << dq.at(i) << endl;
   }

Instead of attempting to read garbage values from memory, the at() function will realize that it is about to overrun the deque and will throw an exception.

Related Topics: [] operator

 
• • • IndexRecent changesRSScc