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>中的坑的更多相关文章

  1. std::vector<bool> 在 auto 推断下的返回值是 bool & 引用

    转自: https://www.cnblogs.com/hustxujinkang/p/5218148.html //////////// std::vector<bool> featur ...

  2. C++ std::vector<bool>

    std::vector template < class T, class Alloc = allocator<T> > class vector; // generic te ...

  3. 说一说vector<bool>

    vector<T>标准库模版类应该是绝大多数c++程序员使用频率比较高的一个类了.不过vector<bool>也许就不那么被程序员所了解.关于vector<bool> ...

  4. STL:vector<bool> 和bitset

    今天某个地方要用到很多位标记于是想着可以用下bitset,不过发现居然是编译时确定空间的,不能动态分配.那就只能用vector来代替一下了,不过发现居然有vector<bool>这个特化模 ...

  5. 《条目十八》避免使用vector<bool>

    <条目十八>避免使用vector 先说结论: 一是:vector<bool>不是标准容器,因为标准容器的对于T *p = &c[0];必须是可编译的. 二是:vecto ...

  6. C++ 中的std::vector介绍(转)

    vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...

  7. 【opencv】cv::Mat转std::vector<cv::Point2d> (注意两容器中数据类型的一致性)

    获取cv::Mat大小: mymat.size() 获取cv::Mat指定位置的值:需指定数据类型,且注意数据类型应与存入时的数据类型一致,否则会导致不抛出异常的数据错误 mymat.at<,i ...

  8. 在WinDbg中显示和搜索std::vector内容

    WinDbg从来都不擅长可视化.尽管Visual Studio一直都有autoexp.dat,而且最近还出现了本机调试器可视化工具,但WinDbg用户不得不满足于转储内存区域和搜索内存来识别模式.另一 ...

  9. 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)

    string.vector 互转 string 转 vector vector  vcBuf;string        stBuf("Hello DaMao!!!");----- ...

随机推荐

  1. nullable,nonnull, null_resettable以及_Null_unspecified的区别和使用

    1.关键字:可以用于属性 方法和返回值参数中 关键字作用:提示作用  告诉开发者属性信息 关键字的目的:迎合swift 强语言,swift必须要指定一个对象是否为空 关键字好处:提高代码规划,减少沟通 ...

  2. Powerdesigner SqlServer转Oracle(转)

    参考文章,原文地址:http://blog.csdn.net/cicada688/article/details/7802881 问题1:sqlserver数据库直接转oracle.字段类型由sql ...

  3. 学习iOS

    最近想学习一下iOS开发,不过一点基础都没有,得先从熟悉object-C语法开始,在此把学习中的一些知识点作为记录. mac pro常用操作 学习ios[1]Objective-C 基本语法 学习io ...

  4. Linux x64 下 Matlab R2013a 300 kb 脚本文件调试的 CPU 占用过高问题的解决办法

    (1) 系统+软件版本 CentOS 6.5 (Final), 64 位,内核initramfs-2.6.32-431.5.1.el6.x86_64, MATLAB Version: 8.1.0.60 ...

  5. js判断数组

    1.constructor 在W3C定义中的定义:constructor 属性返回对创建此对象的数组函数的引用 就是返回对象相对应的构造函数.从定义上来说跟instanceof不太一致,但效果都是一样 ...

  6. 小游戏Step表

    [Config]1|0|0|2|172,120,134|公主的记忆|1|2[Config] [Config]2|1|1|2|172,120,134|公主的记忆|1|2[Config] [Config] ...

  7. jquery选择相同ID

    jQuery中$("#id")只能选择第一个对象,不能选择所有相同id的元素.   通过 $("input[id='xxxx']"); 可以选择多个相同id的元 ...

  8. Android - 设置TextView的字体行间距 - TextView

    xml文件中给TextView添加: android:lineSpacingExtra="10dp"// 行间距 android:lineSpacingMultiplier=&qu ...

  9. 在一个老外微信PM的眼中,中国移动App UI那些事儿

    本文编译自Dan Grover的博客,他现在是腾讯微信的产品经理.以下是他从旧金山搬到广州后的近半年时间里,在试用过微信微博等中国主流移动App后,总结出的中美App在设计理念上的差异,并对中国移动A ...

  10. 写一个程序可以对两个字符串进行测试,得知第一个字符串是否包含在第二个字符串中。如字符串”PEN”包含在字符串“INDEPENDENT”中。

    package lovo.test; import java.util.Scanner; public class Java { @param args public static void main ...