vector::front
벡터의 첫 번째 요소에 대한 참조를 반환합니다.
reference front( ); const_reference front( ) const;
반환 값
벡터 개체의 첫 번째 요소에 대한 참조입니다. 벡터가 비어 있으면 반환이 정의되지 않습니다.
설명
front의 반환 값이 const_reference로 할당되는 경우, 벡터 개체는 수정할 수 없습니다. front의 반환 값이 reference에 할당되는 경우에는 벡터 개체를 수정할 수 있습니다.
_SECURE_SCL 1로 컴파일할 때 빈 벡터의 요소에 액세스하려고 하면 런타임 오류가 발생합니다. 자세한 내용은 Checked Iterators를 참조하세요.
예제
// 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}
출력
The first integer of v1 is 10
Now, the first integer of v1 is 11
요구 사항
헤더: <vector>
네임스페이스: std