共用方式為


vector<bool> 類別

類別 vector<bool> 是 類型 bool 元素的部分特製化 vector 。 它具有特製化所使用基礎類型的配置器,透過每個位元儲存一個 bool 值來提供空間最佳化。

語法

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

備註

此類別範本特製化的行為類似 vector ,但本文中所述的差異除外。

處理 bool 類型的作業會對應至容器儲存體中的值。 allocator_traits::construct 不會用來建構這些值。

Typedefs

類型名稱 描述
const_pointer const_iterator 的 typedef,可做為常數指標指向 vector<bool> 的布林值項目。
const_reference bool 的 typedef。 初始化之後,它不會觀察原始值的更新。
pointer iterator 的 typedef,可做為指標指向 vector<bool> 的布林值項目。

成員函式

成員函數 描述
flip 會反轉 vector<bool> 中的所有位元。
swap 交換兩個 vector<bool> 的項目。
operator[] 傳回在指定位置上 vector<bool> 項目的模擬參考。
at 函式與未指定的 vector ::at 函式相同,不同之處在于它會使用 Proxy 類別 vector<bool>::reference 。 另請參閱 operator[]
front 函式與未指定的 vector ::front 函式相同,但會使用 Proxy 類別 vector<bool>::reference 。 另請參閱 operator[]
back 函式與未指定的 vector ::back 函式相同,不同之處在于它會使用 Proxy 類別 vector<bool>::reference 。 另請參閱 operator[]

Proxy 類別

名稱 描述
vector<bool>::reference 類別,做為 Proxy 以模擬 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> 項目的位置。

傳回值

vector<bool>::referencevector<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;
}
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> Proxy 類別 ,用來模擬 bool&

備註

因為 C++ 原生不允許直接參考位,因此需要模擬參考。 vector<bool> 對每個項目只使用一個位元,您可以使用這個 Proxy 類別來參考位元。 不過,參考模擬並未完成,因為某些指派無效。 例如,因為無法取得物件的位址 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;

傳回值

物件的 專案 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

靜態成員函式,使用 Proxy 類別 vector<bool>::reference 交換布林向量 ( vector<bool> 的兩個元素。

static void swap(
    reference Left,
    reference Right);

參數

Left
要與 Right 項目交換的項目。

Right
要與 Left 項目交換的項目。

備註

這個多載支援 vector<bool> 的特別 Proxy 需求。 vector::swap 的功能與 的單 vector<bool>::swap() 一引數多載相同。

另請參閱

C++ 標準程式庫中的執行緒安全
C++ 標準程式庫參考