다음을 통해 공유


vector::begin

벡터의 첫 번째 요소에 대한 임의 액세스 반복기를 반환합니다.

const_iterator begin() const; iterator begin();

반환 값

첫 번째 요소 또는 빈 vector 다음의 위치를 가리키는 임의 액세스 반복기입니다. 항상 vector::end에서 반복되는 값을 비교하여 유효한지 확인해야 합니다.

설명

begin의 반환 값이 vector::const_iterator에 할당되는 경우에는 vector 개체를 수정할 수 없습니다. begin의 반환 값이 vector::iterator에 할당되는 경우에는 vector 개체를 수정할 수 있습니다.

예제

// vector_begin.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
using namespace std;
int main()
{
    vector<int> vec;
    vector<int>::iterator pos;
    vector<int>::const_iterator cpos;

    vec.push_back(1);
    vec.push_back(2);

    cout << "The vector vec contains elements:";
    for (pos = vec.begin(); pos != vec.end(); ++pos)
    {
        cout << " " << *pos;
    }

    cout << endl;

    cout << "The vector vec now contains elements:";
    pos = vec.begin();
    *pos = 20;
    for (; pos != vec.end(); ++pos)
    {
        cout << " " << *pos;
    }
    cout << endl;

    // The following line would be an error because iterator is const
    // *cpos = 200;}
  

요구 사항

헤더: <vector>

네임스페이스: std

참고 항목

참조

vector 클래스

표준 템플릿 라이브러리