名稱空間
變體
操作

std::vector<bool>

來自 cppreference.com
< cpp‎ | 容器
 
 
 
 
在標頭檔案 <vector> 中定義
template<

    class Allocator

> class vector<bool, Allocator>;

std::vector<bool>bool 型別 std::vector 的可能節省空間的特化。

std::vector<bool> 節省空間的方式(以及是否進行了最佳化)是實現定義的。一種潛在的最佳化涉及合併向量元素,使每個元素佔用一個位而不是 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 的引用的代理類
(類)
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> 的公共成員函式) [編輯]
訪問第一個元素
(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 的值
(函式模板) [編輯]
特化 std::swap 演算法
(函式模板) [編輯]
擦除所有滿足特定標準的元素
(函式模板) [編輯]

[編輯] 輔助類

std::vector<bool> 的雜湊支援
(類模板特化) [編輯]

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

[編輯] 注意

如果位集的尺寸在編譯時已知,可以使用 std::bitset,它提供了更豐富的成員函式集。此外,還存在 boost::dynamic_bitset 作為 std::vector<bool> 的替代方案。

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

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

特性測試 標準 特性
__cpp_lib_containers_ranges 202202L (C++23) 容器的範圍構造和插入

[編輯] 示例

[編輯] 缺陷報告

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

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2187 C++11 bool 的特化缺少 emplaceemplace_back 成員函式 已新增