2017-01-23 2 views
1
#include <iostream> 
#include<vector> 
using namespace std; 
bool a; 
char c; 
int main() { 

    vector<bool> bVec = { true,false,true,false,true}; 
    vector<char> cVec = { 'a', 'b', 'c', 'd', 'e' }; 
    cout<<sizeof(bVec);cout<<endl; 
    cout<<sizeof(cVec); 
    cout<<endl; 
    cout<<sizeof(a); 
    cout<<endl; 
    cout<<sizeof(c); 

    return 0; 
} 

이 코드를 컴파일 할 때 cVec의 크기는 20이고 bvec의 크기는 12입니다. 왜 크기가 다른가요?두 벡터의 크기가 왜 <bool> bVec = {true, false, true, false, true}; 벡터 <char> cVec = { 'a', 'b', 'c', 'd', 'e'}; 다르다?

+1

'std :: vector < bool > '은 동물의 특별한 종류입니다. http://stackoverflow.com/questions/17794569/why-is-vectorbool-not-a-stl-container를 참조하십시오. –

답변

0

std::vector<bool>std::vector의 특수한 경우로 데이터를 공간 효율적으로 저장하고 bool& 대신 프록시 개체를 반환하여 데이터를 조작합니다. 따라서 일반적으로 회원 데이터가 다르며 보통 std::vector이므로 크기 차이가 있습니다.

관련 문제