Scott Meyer的书《Effective STL:提高标准模板库使用效率的50种具体方法》的第18条指出,要避免使用vector <bool>
,因为它不是STL容器,也不能真正容纳bool
。
以下代码:
vector <bool> v;
bool *pb =&v[0];
将不会编译,这违反了STL容器的要求。
错误:
cannot convert 'std::vector<bool>::reference* {aka std::_Bit_reference*}' to 'bool*' in initialization
vector<T>::operator []
返回类型应该是T&
,但为什么它是vector<bool>
的特例
vector<bool>
到底由什么组成?
该项目还说:
deque<bool> v; // is a STL container and it really contains bools
这是否可以用作vector<bool>
的替代方案
有谁能解释一下这个吗?
https://stackoverflow.com/questions/17794569
复制相似问题