I got curious and applied sizeof()
operator on some C++ standard Library Classes. Here is what I have observed:
int main()
{
vector<double> v1;
set<double> s1;
map<double,double> m1;
stack<char> st;
queue<char> q;
vector<char> v2;
set<char> s2;
map<char,char> m2;
cout<<sizeof(v1)<<" "<<sizeof(s1)<<" "<<sizeof(m1)<<endl;
cout<<sizeof(v2)<<" "<<sizeof(s2)<<" "<<sizeof(m2)<<endl;
cout<<sizeof(q)<<" "<<sizeof(st)<<endl;
return 0;
}
The output on my system (64-bit) is:
12 24 24
12 24 24
40 40
I know that std::set
uses Red-Black tree for implementation. So a each node of a binary tree has two pointers(8 bytes each) and the value(8 bytes, total 24) seems alright.
std::map
(also uses Red-Black trees) has an extra key, but still 24 bytes? Why?Why
std::queue
andstd::stack
take 40 bytes whilestd::vector
takes only 12 bytes?Why
char
anddouble
does not affect the size of the class? Is it because of the templates?