std::vector<bool>中的坑
http://www.cplusplus.com/reference/vector/vector/?kw=vector
C++中,vector<bool>为了达到节省内存的目的,专门做了特化,大概方式就是用bit位来存储数组中的元素。代价就是,这个容器里面的内置类型乱掉了:
member type definition notes
value_type The first template parameter (bool)
allocator_type The second template parameter (Alloc) defaults to: allocator<bool>
reference A specific member class (see reference below)
const_reference bool
pointer a type that simulates pointer behavior convertible to const_pointer
const_pointer a type that simulates pointer to const behavior
iterator a type that simulates random access iterator behavior convertible to const_iterator
const_iterator a type that simulates random access iterator to const behavior
reverse_iterator reverse_iterator<iterator>
const_reverse_iterator reverse_iterator<const_iterator>
difference_type a signed integral type usually the same as ptrdiff_t
size_type an unsigned integral type usually the same as size_t
比较常见的问题是,reference 类型的问题(注意这里const_reference的类型是bool,跟reference不一样,简直奇葩)
vector<bool> a;
a[0];//类型并不是bool!!!
由于定义了operator bool(),一般会有隐式类型转换,所以不易察觉。
但是在某些情况下,比如模板,auto自动类型推导里面,得到的类型是严格的类型,不会做转换,这时候就有可能出问题,比如下面这段代码:
auto b = a[0];
for(const auto& c : a)
{
}
std::vector<bool>中的坑的更多相关文章
- std::vector<bool> 在 auto 推断下的返回值是 bool & 引用
转自: https://www.cnblogs.com/hustxujinkang/p/5218148.html //////////// std::vector<bool> featur ...
- C++ std::vector<bool>
std::vector template < class T, class Alloc = allocator<T> > class vector; // generic te ...
- 说一说vector<bool>
vector<T>标准库模版类应该是绝大多数c++程序员使用频率比较高的一个类了.不过vector<bool>也许就不那么被程序员所了解.关于vector<bool> ...
- STL:vector<bool> 和bitset
今天某个地方要用到很多位标记于是想着可以用下bitset,不过发现居然是编译时确定空间的,不能动态分配.那就只能用vector来代替一下了,不过发现居然有vector<bool>这个特化模 ...
- 《条目十八》避免使用vector<bool>
<条目十八>避免使用vector 先说结论: 一是:vector<bool>不是标准容器,因为标准容器的对于T *p = &c[0];必须是可编译的. 二是:vecto ...
- C++ 中的std::vector介绍(转)
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...
- 【opencv】cv::Mat转std::vector<cv::Point2d> (注意两容器中数据类型的一致性)
获取cv::Mat大小: mymat.size() 获取cv::Mat指定位置的值:需指定数据类型,且注意数据类型应与存入时的数据类型一致,否则会导致不抛出异常的数据错误 mymat.at<,i ...
- 在WinDbg中显示和搜索std::vector内容
WinDbg从来都不擅长可视化.尽管Visual Studio一直都有autoexp.dat,而且最近还出现了本机调试器可视化工具,但WinDbg用户不得不满足于转储内存区域和搜索内存来识别模式.另一 ...
- 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)
string.vector 互转 string 转 vector vector vcBuf;string stBuf("Hello DaMao!!!");----- ...
随机推荐
- Starting MySQL... ERROR! The server quit without updating PID file 解决办法
来源:http://blog.rekfan.com/articles/186.html 我使用了第4条解决了问题 1.可能是/usr/local/mysql/data/rekfan.pid文件没有写的 ...
- beat your own python env
1,进入根目录,修改.bashrc,增加一个PATH目录 例如:alias cjtf='export PATH=/home/www/xxx/python_env:$PATH' 如果个人的机器的就不用a ...
- html/css小练习2
效果图:
- guava学习--ratelimiter
RateLimiter类似于JDK的信号量Semphore,他用来限制对资源并发访问的线程数. RateLimiter limiter = RateLimiter.create(4.0); //每秒不 ...
- css外边距margin
- flash与js交互
当Flash置于HTML容器中时,经常会遇到Flash与JS的通信问题,例如:JS能否调用Flash中的变量.方法,Flash能否调用JS中的变量.方法等等.答案是肯定的.随着技术的不断发展,解决方案 ...
- 1745. Yet Another Answer
http://acm.timus.ru/problem.aspx?space=1&num=1745 题目大意: 可以是任意的顺序,在满足括号匹配的情况下,求组合成的字符串长度最长 思路: 先将 ...
- 【Python⑤】python序列---list和tuple
sequence 序列 sequence(序列)是一组有顺序的对象的集合.序列可以包含一个或多个元素,也可以没有任何元素. 我们之前所说的基本数据类型,都可以作为序列的对象.对象还可以是另一个序列.序 ...
- Emoji表情符号录入MySQL数据库失败解决
让MySQL支持Emoji表情,涉及无线相关的 MySQL 数据库建议都提前采用 utf8mb4 字符集. utf8mb4和utf8到底有什么区别呢?原来以往的mysql的utf8一个字符最多3字节, ...
- Uart、SPI和I2C的区别
串口通信:UART.SPI.I2C区别[引用] 1.UART就是两线,一根发送一根接收,可以全双工通信,线数也比较少.数据是异步传输的,对双方的时序要求比较严格,通信速度也不是很快.在多机通信上面 ...