sizeof() with vectors

hi!

1
2
  vector<bool> b(9,true);
  cout<<sizeof(b); //show 40 and not 2 


why it show 40?, i hope it show 2 because vector<bool> use a bit.
//sorry for my bad english :v
Hi,

vector<bool> may not necessarily use a bit, maybe you need std::bitset ?


Why are you worried about the sizeof it? You have already specified you want 9 of them. There are a bunch of member functions to get info you might want.

http://en.cppreference.com/w/cpp/container/vector_bool
http://en.cppreference.com/w/cpp/utility/bitset
http://www.cplusplus.com/reference/bitset/bitset/


good Luck!!
sizeof is computed at compile time.
all objects of the same class have the same sizeof, its state is irrelevant.

A vector does not store the data, but pointers that tell where the data is. So when computing sizeof() you don't get the size of the data, but of the pointers.
Topic archived. No new replies allowed.