Megosztás a következőn keresztül:


vector::front

Returns a reference to the first element in a vector.

reference front( ); 
const_reference front( ) const;

Return Value

A reference to the first element in the vector object. If the vector is empty, the return is undefined.

Remarks

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

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

Example

// vector_front.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
using namespace std;  
int main( )
{
  vector <int> vec;
    vec.push_back(10);
    vec.push_back(20);

    int& i = vec.front();
    const int& ci = vec.front();

    cout << "The value of vec[0] is " << i << endl;

    // front() returns a reference, not an iterator
    // by incrementing i, we change the value of the first element
    i++;
    cout << "Now, the value of vec[0] is " << i << endl;

   // ci++; compiler error because ci is const}

Output

The first integer of v1 is 10
Now, the first integer of v1 is 11

Requirements

Header: <vector>

Namespace: std

See Also

Reference

vector Class

vector::front and vector::back

Standard Template Library