vector<bool> 클래스

클래스는 vector<bool> 형식 bool요소에 vector 대한 부분 특수화입니다. 특수화(비트당 하나의 bool 값을 저장하여 공간 최적화 제공)에서 사용하는 기본 형식에 대한 할당자가 있습니다.

구문

template <class Allocator = allocator<bool>>
class vector<bool, Allocator>

설명

이 클래스 템플릿 특수화는 이 문서에 설명된 차이점을 제외하고 다음과 같이 vector동작합니다.

bool 형식으로 처리되는 작업은 컨테이너 스토리지의 값에 해당합니다. allocator_traits::construct 는 이러한 값을 생성하는 데 사용되지 않습니다.

Typedef

형식 이름 설명
const_pointer const_iterator의 부울 요소에 대한 상수 포인터로 사용할 수 있는 vector<bool>에 대한 typedef입니다.
const_reference bool에 대한 typedef입니다. 초기화 후에는 원래 값에 대한 업데이트를 관찰하지 않습니다.
pointer iterator의 부울 요소에 대한 포인터로 사용할 수 있는 vector<bool>에 대한 typedef입니다.

멤버 함수

멤버 함수 설명
flip vector<bool>의 모든 비트를 반대로 바꿉니다.
swap vector<bool>의 요소를 교환합니다.
operator[] 지정된 위치에서 vector<bool> 요소에 대한 시뮬레이션 참조를 반환합니다.
at 프록시 클래스vector<bool>::reference를 사용한다는 점을 제외하면 지정vector되지 않은 ::at 함수와 동일한 함수입니다. 또한 operator[]도 참조하세요.
front 프록시 클래스vector<bool>::reference를 사용한다는 점을 제외하고 지정vector되지 않은 ::front 함수와 동일한 함수입니다. 또한 operator[]도 참조하세요.
back 프록시 클래스vector<bool>::reference를 사용한다는 점을 제외하고, 지정vector되지 않은 ::back 함수와 동일한 함수입니다. 또한 operator[]도 참조하세요.

프록시 클래스

이름 설명
vector<bool>::reference 클래스 bool& 동작을 시뮬레이션하기 위해 프록시 역할을 하며, 해당 개체가 vector<bool> 개체 내 요소(단일 비트)에 대한 참조를 제공할 수 있는 클래스입니다.

요구 사항

헤더: <vector>

네임스페이스:std

vector<bool>::const_pointer

vector<bool> 개체에 포함된 시퀀스의 부울 요소에 대해 고정 포인터로 사용할 수 있는 개체를 설명하는 형식입니다.

typedef const_iterator const_pointer;

vector<bool>::const_reference

vector<bool> 개체에 포함된 시퀀스의 부울 요소에 대해 고정 참조로 사용할 수 있는 개체를 설명하는 형식입니다.

typedef bool const_reference;

설명

자세한 내용 및 코드 예제는 다음을 참조하세요 vector<bool>::reference::operator=.

vector<bool>::flip

vector<bool>의 모든 비트를 반대로 바꿉니다.

void flip();

예시

// vector_bool_flip.cpp
// compile with: /EHsc /W4
#include <vector>
#include <iostream>

int main()
{
    using namespace std;
    cout << boolalpha; // format output for subsequent code

    vector<bool> vb = { true, false, false, true, true };
    cout << "The vector is:" << endl << "    ";
    for (const auto& b : vb) {
        cout << b << " ";
    }
    cout << endl;

    vb.flip();

    cout << "The flipped vector is:" << endl << "    ";
    for (const auto& b : vb) {
        cout << b << " ";
    }
    cout << endl;
}

vector<bool>::operator[]

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

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

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

매개 변수

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

Return Value

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

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

설명

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

예시

이 코드 예제에서는 주석 처리되는 두 가지 일반적인 코딩 실수와 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;
}
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<bool>::pointer

vector<bool> 개체에 포함된 시퀀스의 부울 요소에 대해 포인터로 사용할 수 있는 개체를 설명하는 형식입니다.

typedef iterator pointer;

vector<bool>::reference 클래스

클래스는 vector<bool>::reference 시뮬레이트하기 위해 클래스에서 vector<bool> 제공하는 프록시 클래스 입니다 bool&.

설명

C++는 기본적으로 비트에 대한 직접 참조를 허용하지 않으므로 시뮬레이션된 참조가 필요합니다. vector<bool>는 요소당 1비트만 사용하며, 이 프록시 클래스만 사용하여 참조할 수 있습니다. 그러나 특정 할당이 유효하지 않으므로 참조 시뮬레이션이 완료되지 않습니다. 예를 들어 개체의 vector<bool>::reference 주소를 사용할 수 없으므로 사용하는 다음 코드가 vector<bool>::operator[] 올바르지 않습니다.

vector<bool> vb;
//...
bool* pb = &vb[1]; // conversion error - do not use
bool& refb = vb[1];   // conversion error - do not use

vector<bool>::reference::flip

참조된 vector<bool> 요소의 부울 값을 반전합니다.

void flip();

예시

// vector_bool_ref_flip.cpp
// compile with: /EHsc /W4
#include <vector>
#include <iostream>

int main()
{
    using namespace std;
    cout << boolalpha;

    vector<bool> vb = { true, false, false, true, true };

    cout << "The vector is: " << endl << "    ";
    for (const auto& b : vb) {
        cout << b << " ";
    }
    cout << endl;

    vector<bool>::reference vbref = vb.front();
    vbref.flip();

    cout << "The vector with first element flipped is: " << endl << "    ";
    for (const auto& b : vb) {
        cout << b << " ";
    }
    cout << endl;
}
The vector is:
    true false false true true
The vector with first element flipped is:
    false false false true true

vector<bool>::reference::operator bool

vector<bool>::referencebool로 묵시적으로 변환합니다.

operator bool() const;

Return Value

개체 요소의 부울 값입니다 vector<bool> .

설명

vector<bool> 연산자는 개체를 수정할 수 없습니다.

vector<bool>::reference::operator=

비트에 부울 값을 할당하거나 참조된 요소에 저장된 값을 비트에 할당합니다.

reference& operator=(const reference& Right);
reference& operator=(bool Val);

매개 변수

Right
해당 값을 비트에 할당할 요소 참조입니다.

Val
비트에 할당될 부울 값입니다.

예시

// vector_bool_ref_op_assign.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
#include <string>

using namespace std;

template <typename C> void print(const string& s, const C& c) {
    cout << s;

    for (const auto& e : c) {
        cout << e << " ";
    }

    cout << endl;
}

int main()
{
    cout << boolalpha;

    vector<bool> vb = { true, false, false, true, true };

    print("The vector is: ", vb);

    // Invoke vector<bool>::reference::operator=()
    vector<bool>::reference refelem1 = vb[0];
    vector<bool>::reference refelem2 = vb[1];
    vector<bool>::reference refelem3 = vb[2];

    bool b1 = refelem1;
    bool b2 = refelem2;
    bool b3 = refelem3;
    cout << "The original value of the 1st element stored in a bool: " << b1 << endl;
    cout << "The original value of the 2nd element stored in a bool: " << b2 << endl;
    cout << "The original value of the 3rd element stored in a bool: " << b3 << endl;
    cout << endl;

    refelem2 = refelem1;

    print("The vector after assigning refelem1 to refelem2 is now: ", vb);

    refelem3 = true;

    print("The vector after assigning false to refelem1 is now: ", vb);

    // The initial values are still stored in the bool variables and remained unchanged
    cout << "The original value of the 1st element still stored in a bool: " << b1 << endl;
    cout << "The original value of the 2nd element still stored in a bool: " << b2 << endl;
    cout << "The original value of the 3rd element still stored in a bool: " << b3 << endl;
    cout << endl;
}
The vector is: true false false true true
The original value of the 1st element stored in a bool: true
The original value of the 2nd element stored in a bool: false
The original value of the 3rd element stored in a bool: false

The vector after assigning refelem1 to refelem2 is now: true true false true true
The vector after assigning false to refelem1 is now: true true true true true
The original value of the 1st element still stored in a bool: true
The original value of the 2nd element still stored in a bool: false
The original value of the 3rd element still stored in a bool: false

vector<bool>::swap

프록시 클래스vector<bool>::reference를 사용하여 부울 벡터(vector<bool>)의 두 요소를 교환하는 정적 멤버 함수입니다.

static void swap(
    reference Left,
    reference Right);

매개 변수

Left
Right 요소와 교환할 요소입니다.

Right
Left 요소와 교환할 요소입니다.

설명

이 오버로드는 vector<bool>의 특정 프록시 요구 사항을 지원합니다. vector::swap의 단일 인수 오버로드와 동일한 기능이 있습니다 vector<bool>::swap().

참고 항목

C++ 표준 라이브러리의 스레드 보안
C++ 표준 라이브러리 참조