다음을 통해 공유


vector::at

Returns a reference to the element at a specified location in the vector.

reference at( 
   size_type _Pos 
); 
const_reference at( 
   size_type _Pos 
) const;

Parameters

  • _Pos
    The subscript or position number of the element to reference in the vector.

Return Value

A reference to the element subscripted in the argument. If _Off is greater than the size of the vector, at throws an std::out_of_range exception.

Remarks

If the return value of at is assigned to a const_reference, the element it points to cannot be modified. If the return value of at is assigned to a reference, the vector object can be modified.

Example

// vector_at.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
using namespace std;

int main( )
{
      vector <int> vec;
    vec.push_back(10);
    vec.push_back(20);

    const int &i = vec.at(0);
    int &j = vec.at(1);
    cout << "The first element is " << i << endl;
    cout << "The second element is " << j << endl;
}
The first element is 10
The second element is 20

Requirements

Header: <vector>

Namespace: std

See Also

Reference

vector Class

Standard Template Library