Is there a way to get the byte size of vectors through type alone?
c++, sizeof, vector
Solution
Since `vector<>` itself is a class that does its own dynamic memory management, using the `sizeof` operator to ask it about size is not terribly meaningful. I suspect you will find that the value you calculate above will always be 12.
You can ask a vector how many elements it contains using the `.size()` method. Also, the `.capacity()` method will tell you how many elements it has actually allocated memory for (even if they're not all in use yet).
Remember that `sizeof` is evaluated at compile time, so it cannot know how many elements are inserted into the container later, at run time.
Problem
How can I predict the size of a vector? ``` #include <vector> #include <iostream> using namespace std; int main() { cout << sizeof(vector<char[8]>) << endl; cout << sizeof(vector<char[16]>) << endl; return 0; } [starlon@localhost LCDControl]$ ./test 12 12 ```