std::vector<bool>
來自 cppreference.com
在標頭檔案 <vector> 中定義 |
||
template< class 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
|
| ||||
const_iterator
|
| ||||
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> 的公共成員函式) | |
(C++23) |
將一個範圍的值賦給容器 ( 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> 的公共成員函式) |
(C++11) |
返回指向末尾的迭代器 ( std::vector<T,Allocator> 的公共成員函式) |
(C++11) |
返回指向起始的逆向迭代器 ( 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> 的公共成員函式) | |
(C++23) |
插入元素範圍 ( std::vector<T,Allocator> 的公共成員函式) |
(C++23) |
新增一個元素範圍到結尾 ( std::vector<T,Allocator> 的公共成員函式) |
(C++11) |
就地構造元素 ( 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<bool>:: reference(公共靜態成員函式) |
[編輯] 非成員函式
(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(在 C++20 中移除)(C++20) |
字典序比較兩個 vector 的值(函式模板) |
特化 std::swap 演算法 (函式模板) | |
擦除所有滿足特定標準的元素 (函式模板) |
[編輯] 輔助類
(C++11) |
對 std::vector<bool> 的雜湊支援 (類模板特化) |
[編輯] 推導指南 (C++17)
[編輯] 注意
如果位集的尺寸在編譯時已知,可以使用 std::bitset,它提供了更豐富的成員函式集。此外,還存在 boost::dynamic_bitset
作為 std::vector
<bool> 的替代方案。
由於其表示可能經過最佳化,std::vector
<bool> 不一定滿足所有 Container 或 SequenceContainer 的要求。例如,由於 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 的特化缺少 emplace 和 emplace_back 成員函式 |
已新增 |