c++ 11标准模板(STL) std::vector<bool> (一)

定义于头文件 <vector>

template<class Allocator>
class vector<bool, Allocator>;

std::vector<bool> 是 std::vector 对类型 bool 为空间提效的特化。

std::vector<bool> 中对空间提效的行为(以及它是否有优化)是实现定义的。一种潜在优化涉及到 vector 的元素联合,使得每个元素占用一个单独的位,而非 sizeof(bool) 字节。

std::vector<bool> 表现类似 std::vector ,但为节省空间,它:

  • 不必作为连续数组存储元素(故 &v[0] + n != &v[n] )
  • 暴露类 std::vector<bool>::reference 为访问单个位的方法。尤其是,此类型的类为 operator[] 以值返回。
  • 不使用 std::allocator_traits::construct 构造位值。
  • 不保证同一容器中的不同元素能由不同线程同时修改。

成员类型

成员类型 定义
value_type bool
allocator_type Allocator
size_type 实现定义
difference_type 实现定义

reference

表示到单个 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> 的公开成员函数)

operator=

赋值给容器
(std::vector<T,Allocator> 的公开成员函数)

assign

将值赋给容器
(std::vector<T,Allocator> 的公开成员函数)

get_allocator

返回相关的分配器
(std::vector<T,Allocator> 的公开成员函数)

元素访问

at

访问指定的元素,同时进行越界检查
(std::vector<T,Allocator> 的公开成员函数)

operator[]

访问指定的元素
(std::vector<T,Allocator> 的公开成员函数)

front

访问第一个元素
(std::vector<T,Allocator> 的公开成员函数)

back

访问最后一个元素
(std::vector<T,Allocator> 的公开成员函数)

迭代器

begin cbegin

返回指向容器第一个元素的迭代器
(std::vector<T,Allocator> 的公开成员函数)

end cend

返回指向容器尾端的迭代器
(std::vector<T,Allocator> 的公开成员函数)

rbegin crbegin

返回指向容器最后元素的逆向迭代器
(std::vector<T,Allocator> 的公开成员函数)

rend crend

返回指向前端的逆向迭代器
(std::vector<T,Allocator> 的公开成员函数)

容器

empty

检查容器是否为空
(std::vector<T,Allocator> 的公开成员函数)

size

返回容纳的元素数
(std::vector<T,Allocator> 的公开成员函数)

max_size

返回可容纳的最大元素数
(std::vector<T,Allocator> 的公开成员函数)

reserve

预留存储空间
(std::vector<T,Allocator> 的公开成员函数)

capacity

返回当前存储空间能够容纳的元素数
(std::vector<T,Allocator> 的公开成员函数)

修改器

clear

清除内容
(std::vector<T,Allocator> 的公开成员函数)

insert

插入元素
(std::vector<T,Allocator> 的公开成员函数)

emplace

(C++14 起)

原位构造元素
(std::vector<T,Allocator> 的公开成员函数)

erase

擦除元素
(std::vector<T,Allocator> 的公开成员函数)

push_back

将元素添加到容器末尾
(std::vector<T,Allocator> 的公开成员函数)

emplace_back

(C++14)

在末尾原位构造元素
(std::vector<T,Allocator> 的公开成员函数)

pop_back

移除末元素
(std::vector<T,Allocator> 的公开成员函数)

resize

改变容器中可存储元素的个数
(std::vector<T,Allocator> 的公开成员函数)

swap

交换内容
(std::vector<T,Allocator> 的公开成员函数)

vector<bool> 特定修改器

flip

翻转所有位
(公开成员函数)

swap

[静态]

交换二个 std::vector<bool>::reference
(公开静态成员函数)

非成员函数

operator==operator!=operator<operator<=operator>operator>=

按照字典顺序比较 vector 中的值
(函数模板)

std::swap(std::vector)

特化 std::swap 算法
(函数模板)

帮助类

std::hash<std::vector<bool>>

(C++11)

std::vector<bool> 的哈希支持
(类模板特化)

注意

若位集的大小在编译时已知,可使用 std::bitset ,它提供一组更丰富的成员函数。另外, boost::dynamic_bitset 作为 std::vector<bool> 的替用者存在。

因为 std::vector<bool> 的表示可以优化,故它不需要满足所有容器 (Container) 或序列容器 (SequenceContainer) 要求。例如,因为 std::vector<bool>::iterator 是实现定义的,故它可以不满足 遗留向前迭代器 (LegacyForwardIterator) 要求。使用要求 遗留向前迭代器 (LegacyForwardIterator) 的算法,例如 std::search 可能导致编译时或运行时错误

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/130544960