다음을 통해 공유


vector<bool>::operator[]

지정된 위치에서 vector<bool> 요소에 대한 시뮬레이션 참조를 반환합니다.

vector<bool>::reference operator[](
   size_type Pos
);
vector<bool>::const_reference operator[](
   size_type Pos
) const;

매개 변수

매개 변수

설명

Pos

vector<bool> 요소의 위치입니다.

반환 값

인덱싱된 요소의 값이 포함된 vector<bool>::reference 또는 vector<bool>::const_reference 개체입니다.

지정된 위치가 컨테이너의 크기보다 크거나 같을 경우 결과가 정의되지 않습니다.

설명

_ITERATOR_DEBUG_LEVEL 집합을 사용하여 컴파일할 경우 벡터 경계 밖에서 요소를 액세스하려고 하면 런타임 오류가 발생합니다. 자세한 내용은 Checked Iterators을 참조하십시오.

예제

이 코드 예제는 **vector<bool>::operator[]**의 올바른 사용 방법과 두 가지 흔한 코딩 실수(주석 처리)를 보여 줍니다. 이러한 실수는 **vector<bool>::operator[]**에서 반환하는 vector<bool>::reference 개체의 주소를 가져올 수 없으므로 오류가 발생합니다.

// cl.exe /EHsc /nologo /W4 /MTd 
#include <vector>
#include <iostream>

int main()
{
    using namespace std;
    cout << boolalpha;
    vector<bool> vb;

    vb.push_back(true);
    vb.push_back(false);

    //    bool* pb = &vb[1]; // conversion error - do not use
    //    bool& refb = vb[1];   // conversion error - do not use
    bool hold = vb[1];
    cout << "The second element of vb is " << vb[1] << endl;
    cout << "The held value from the second element of vb is " << hold << endl;

    // Note this doesn't modify hold.
    vb[1] = true;
    cout << "The second element of vb is " << vb[1] << endl;
    cout << "The held value from the second element of vb is " << hold << endl;
}

Output

The second element of vb is false
The held value from the second element of vb is false
The second element of vb is true
The held value from the second element of vb is false

요구 사항

헤더: <vector>

네임스페이스: std

참고 항목

참조

vector<bool> 클래스

표준 템플릿 라이브러리