简体   繁体   English

std :: vector <bool> resize()的未定义行为

[英]Undefined behavior for std::vector<bool> resize()

#include <vector>
int main()
{
    try {
        std::vector<bool> a;
        a.resize(a.max_size() - 100000000);
    }
    catch (...) {
        return -1;
    }
    return 0;
}

I tried to run this program with VS2015 x86, but it crashed in resize() with "Access violation". 我尝试用VS2015 x86运行这个程序,但它在resize()遇到了“访问冲突”。 I wonder if there is any undefined behavior? 我想知道是否有任何未定义的行为? What does the C++ standard say about this? C ++标准对此有何看法?

vector<bool> doesn't always behave like a vector, and it never actually stores bools, but a packed representation of bools that is designed to save space(bitfields). vector<bool>并不总是像向量一样,它实际上从不存储vector<bool> ,而是一个bools的压缩表示,旨在节省空间(bitfields)。 vector<bool> doesn't satisfy the requirements of an STL container, you're best off not using it. vector<bool>不满足STL容器的要求,最好不要使用它。 deque<bool> and bitset are alternatives data structures that will almost certainly satisfy your need for the capabilities promised by vector<bool> . deque<bool>和bitset是替代数据结构,几乎可以满足您对vector<bool>承诺的功能的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM