命名空間
變體
動作

std::vector<bool>

出自 cppreference.com
< cpp‎ | container
 
 
 
 
定義於標頭檔 <vector>
template<

    class Allocator

> class vector<bool, Allocator>;

std::vector<bool> 是針對 bool 類型對 std::vector 所做的一種可能具備空間效率的特化版本。

std::vector<bool> 實現空間效率的方式(以及是否真的有進行優化)是由實作定義的。一種潛在的優化方式是將 vector 元素合併,使得每個元素只佔用一個位元(bit),而不是 sizeof(bool) 個位元組。

std::vector<bool> 的行為與 std::vector 類似,但為了達到空間效率,它:

  • 不一定以連續陣列的形式儲存其元素。
  • 公開了 std::vector<bool>::reference 類別作為存取單個位元的方法。特別是,此類別的物件是由 operator[] 以傳值方式回傳的。
  • 不使用 std::allocator_traits::construct 來建構位元值。
  • 不保證同一個容器中的不同元素可以被不同的執行緒同時修改。

目錄

[編輯] 成員類型

成員型別 定義
value_type bool[編輯]
allocator_type Allocator[編輯]
size_type 由實作定義[編輯]
difference_type 由實作定義[編輯]
代表單個 bool 參照的代理類別
(class)
const_reference bool[編輯]
pointer 由實作定義[編輯]
const_pointer 由實作定義[編輯]
iterator

實作定義的

(直到 C++20)

由實作定義 ConstexprIterator

(自 C++20 起)
[編輯]
const_iterator

實作定義的

(直到 C++20)

由實作定義 ConstexprIterator

(自 C++20 起)
[編輯]
reverse_iterator std::reverse_iterator<iterator>[編輯]
const_reverse_iterator std::reverse_iterator<const_iterator>[編輯]

[編輯] 成員函式

建構 vector
(std::vector<T,Allocator> 的公開成員函式) [編輯]
解構 vector
(std::vector<T,Allocator> 的公開成員函式) [編輯]
指派值給容器
(std::vector<T,Allocator> 的公開成員函式) [編輯]
指派值給容器
(std::vector<T,Allocator> 的公開成員函式) [編輯]
指派數值範圍給容器
(std::vector<T,Allocator> 的公開成員函式) [編輯]
回傳關聯的配置器
(std::vector<T,Allocator> 的公開成員函式) [編輯]
元素存取
存取指定元素,並進行邊界檢查
(std::vector<T,Allocator> 的公開成員函式) [編輯]
存取指定元素
(std::vector<T,Allocator> 的公開成員函式) [edit]
存取第一個元素
(std::vector<T,Allocator> 的公開成員函式) [編輯]
存取最後一個元素
(std::vector<T,Allocator> 的公開成員函式) [編輯]
迭代器
回傳指向起點的反覆器
(std::vector<T,Allocator> 的公開成員函式) [編輯]
(C++11)
回傳指向終點的反覆器
(std::vector<T,Allocator> 的公開成員函式) [編輯]
回傳指向起點的反向反覆器
(std::vector<T,Allocator> 的公開成員函式) [編輯]
(C++11)
回傳指向終點的反向反覆器
(std::vector<T,Allocator> 的公開成員函式) [編輯]
容量
檢查容器是否為空
(std::vector<T,Allocator> 的公開成員函式) [編輯]
回傳元素個數
(std::vector<T,Allocator> 的公開成員函式) [編輯]
回傳最大可能的元素個數
(std::vector<T,Allocator> 的公開成員函式) [編輯]
預留儲存空間
(std::vector<T,Allocator> 的公開成員函式) [編輯]
傳回目前已分配儲存空間中可容納的元素數量
(std::vector<T,Allocator> 的公開成員函式) [編輯]
修改器
清除內容
(std::vector<T,Allocator> 的公開成員函式) [編輯]
插入元素
(std::vector<T,Allocator> 的公開成員函式) [編輯]
插入數值範圍元素
(std::vector<T,Allocator> 的公開成員函式) [編輯]
新增數值範圍元素到結尾
(std::vector<T,Allocator> 的公開成員函式) [編輯]
(C++11)
原地建構元素
(std::vector<T,Allocator> 的公開成員函式) [編輯]
刪除元素
(std::vector<T,Allocator> 的公開成員函式) [編輯]
新增元素到結尾
(std::vector<T,Allocator> 的公開成員函式) [編輯]
原地建構一個元素到結尾
(std::vector<T,Allocator> 的公開成員函式) [編輯]
移除最後一個元素
(std::vector<T,Allocator> 的公開成員函式) [編輯]
更改儲存的元素個數
(std::vector<T,Allocator> 的公開成員函式) [編輯]
交換內容
(std::vector<T,Allocator> 的公開成員函式) [編輯]
vector<bool> 專用修改器
翻轉所有位元
(公開成員函式) [編輯]
[靜態]
交換兩個 std::vector<bool>::reference
(公開靜態成員函式) [編輯]

[編輯] 非成員函式

(於 C++20 中移除)(於 C++20 中移除)(於 C++20 中移除)(於 C++20 中移除)(於 C++20 中移除)(自 C++20 起)
按字典序比較兩個 vector 的值
(函式模板) [edit]
特化 std::swap 演算法
(函式範本) [編輯]
移除所有符合特定條件的元素
(函式樣板) [編輯]

[編輯] 輔助類別

std::vector<bool> 提供雜湊(hash)支援
(類別樣板特化) [編輯]

[編輯] 推導指引 (C++17)

[編輯] 附註

若位元集的大小在編譯時期已知,可以使用 std::bitset,它提供更豐富的成員函式。此外,boost::dynamic_bitset 可作為 std::vector<bool> 的替代方案。

由於其表示方式可能經過優化,std::vector<bool> 不一定滿足所有 ContainerSequenceContainer 的要求。例如,因為 std::vector<bool>::iterator 是由實作定義的,它可能不滿足 LegacyForwardIterator 的要求。使用諸如需要 LegacyForwardIteratorstd::search 等演算法,可能會導致 編譯時期或執行時期的錯誤

Boost.Container 版本的 vector 不對 bool 進行特化。

功能測試巨集 數值 標準 功能
__cpp_lib_containers_ranges 202202L (C++23) 容器的範圍建構與插入

[編輯] 範例

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯應用於之前的 C++ 標準。

DR 應用於 出版時的行為 正確的行為
LWG 2187 C++11 針對 bool 的特化版本缺少 emplaceemplace_back 成員函式 已新增
English Deutsch 日本語 中文(简体) 中文(繁體)