C++STL容器大小
简单测了一下,容器内部元素不会计入容器内存。
#include <iostream>
#include <typeinfo>
#include <cxxabi.h>
#include <memory>
#include <cassert>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <stack>
using namespace std;
shared_ptr<char> demangle(const char* name) {
int status;
char* ret = abi::__cxa_demangle(name, 0, 0, &status);
assert(status == 0 && "demangle error");
shared_ptr<char> retval;
retval.reset((char*)ret, [](char* mem) { if (mem) free((void*)mem); });
return retval;
}
#define SIZEOF(x) cout << ::demangle(typeid((v)).name()) << ": " << sizeof((x)) << endl;
int main() {
vector<int> v{1,2,3};
list<int> l{1,2,3};
deque<int> d{1,2,3};
queue<int> q(d);
stack<int> s(d);
SIZEOF(v);
SIZEOF(l);
SIZEOF(d);
SIZEOF(q);
SIZEOF(s);
return 0;
}