std::vector<bool, Alloc>::reference
来自 cppreference.cn
< cpp | container | vector bool
class reference; |
||
std::vector
<bool, Alloc> 特化定义了 std::vector
<bool, Alloc>::reference 为一个可公开访问的嵌套类。std::vector
<bool, Alloc>::reference 代理了 std::vector
<bool, Alloc> 中对单个比特的引用的行为。
std::vector
<bool, Alloc>::reference 的主要用途是提供一个可以从 operator[] 返回的左值。
任何通过 std::vector
<bool, Alloc>::reference 对 vector 的读取或写入都可能读取或写入整个底层 vector。
[编辑] 成员函数
(构造函数) |
构造引用 (公共成员函数) |
(析构函数) |
销毁引用 (公共成员函数) |
operator= |
为引用的位赋值 (公共成员函数) |
operator bool |
返回引用的位 (公共成员函数) |
flip |
翻转引用的位 (公共成员函数) |
std::vector<bool, Alloc>::reference::reference
reference( const reference& ) = default; |
(自 C++11 起) (constexpr 自 C++20 起) |
|
从另一个引用构造引用。 复制构造函数是隐式声明的。(C++11 前)
其他构造函数只能由 std::vector
<bool, Alloc> 访问。
std::vector<bool, Alloc>::reference::~reference
~reference(); |
(constexpr 自 C++20 起) | |
销毁引用。
std::vector<bool, Alloc>::reference::operator=
reference& operator=( bool x ); |
(1) | (noexcept 自 C++11 起) (constexpr 自 C++20 起) |
constexpr const reference& operator=( bool x ) const noexcept; |
(2) | (自 C++23 起) |
reference& operator=( const reference& x ); |
(3) | (noexcept 自 C++11 起) (constexpr 自 C++20 起) |
为引用的位赋值。
参数
x | - | 要赋值的值 |
返回值
*this
std::vector<bool, Alloc>::reference::operator bool
operator bool() const; |
(noexcept 自 C++11 起) (constexpr 自 C++20 起) |
|
返回引用的位的值。
返回值
引用的位。
std::vector<bool, Alloc>::reference::flip
void flip(); |
(noexcept 自 C++11 起) (constexpr 自 C++20 起) |
|
反转引用的位。
[编辑] 辅助类
std::formatter<std::vector<bool, Alloc>::reference>
template< class T, class CharT > requires /*is-vector-bool-reference*/<T> |
(自 C++23 起) | |
为 std::vector
<bool, Alloc>::reference 特化 std::formatter。此特化使用 std::formatter<bool, CharT> 作为其底层格式器(记为 underlying_
),其中引用的位被转换为 bool 以进行格式化。
仅用于演示的常量 /*is-vector-bool-reference*/<T> 当且仅当 T
表示类型 std::vector
<bool, Alloc>::reference,对于某个类型 Alloc
且 std::vector
<bool, Alloc> 不是程序定义的特化时为 true。
成员函数
template< class ParseContext > constexpr ParseContext::iterator parse( ParseContext& ctx ); |
(1) | (自 C++23 起) |
template< class FormatContext > FormatContext::iterator format( const T& r, FormatContext& ctx ) const; |
(2) | (自 C++23 起) |
1) 等价于 return
underlying_
.parse(ctx);。2) 等价于 return
underlying_
.format(r, ctx);。[编辑] 示例
本节尚不完整 原因:没有示例 |
[编辑] 参见
访问指定的元素 ( std::vector<T,Allocator> 的公共成员函数) | |
[静态] |
交换两个 std::vector<bool>:: reference(公共静态成员函数) |
[编辑] 外部链接
Scott Meyers 的《Effective Modern C++》(2015 年),第 2 章,第 6 项:“当 auto 推导出不期望的类型时,使用显式类型初始化惯用法。”(第 43-46 页)— 描述了代理类 std::vector<bool>::reference 的一种可能的误用)。 |