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;
}

When i compile this code i get size of cVec as 20 and size of bvec as 12 . but why the sizes are different?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Naveen Pawadi
  • 121
  • 1
  • 2
  • 1
    `std::vector< bool >` is a special kind of.. animal. See http://stackoverflow.com/questions/17794569/why-is-vectorbool-not-a-stl-container – Kiril Kirov Jan 23 '17 at 09:48

1 Answers1

0

std::vector<bool> is a special case of std::vector which stores the data in a space-efficient manner and returns proxy objects instead of bool& to manipulate the data. As such, it'll generally have different member data then a normal std::vector, hence the size difference.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193