tags:

views:

375

answers:

6
+1  Q: 

sizeof() a vector

Hello! I have a vector<set<char> > data structure (transactions database) and I want to know the size of it. When I use sizeof() with each set<char> the size is 24 in spite of the set contains 3, 4 or 5 chars. Later, when I use sizeof() with the vector<set<char> > the size is 12... I suppose this is not the way to know the size of a data structure. Any help? Thanks.

+2  A: 

You want vector::size() and set::size().

Assuming v is your vector, do this:

size_t size = 0;
for (vector<set<char> >::const_iterator cit = v.begin(); cit != v.end(); ++cit) {
    size += cit->size();
}

sizeof() is giving you the in-memory size of the object/type it is applied to, in multiples of sizeof(char) (usually one byte). If you want to know the in-memory size of the container and its elements, you could do this:

sizeof(v) + sizeof(T) * v.capacity(); // where T is the element type

What if he has already reserved his vector?
John
@John: What if he *has*? `reserve()` has *nothing* to do with `size()`.
I think John means that if you want some idea how much memory the vector (and contents) occupies, you should be looking at the capacity, not the size. "in-memory size of the container and its elements" is of course ambiguous, but I suspect it's more likely to be understood as relating to the amount of memory allocated by the vector, rather than the amount of that memory currently occupied by elements.
Steve Jessop
+1 for code illustrating how to actually get the number of chars in the nested container.
Mark B
@Steve and @John: You are right. My mistake. I'll update my answer to take capacity into account. I guess I interpreted John's comment to be relating to the number of elements, not the size in memory.
+1  A: 

sizeof() is computed at compile time, so there is no way it can tell you how many elements it has inside.

Use the size() method of the vector object.

Nicolás
+6  A: 

sizeof returns size of object itself. if it contains pointer to array for example it will not count size of array, it will count only size of pointer (4 on 32 bits) for vector use .size

Andrey
A: 

Use vector::size() member function to find out number of items in the vector. Hint - they are allocated on the free store.

Nikolai N Fetissov
+4  A: 

Vector is implemented using internal pointers to the actual storage. Hence sizeof() will always return the same result which does not include the data storage itself. Try using the vector::size() method instead. This will return the number of elements in the vector.

Stephen Doyle
+1  A: 

vector in STL is a class template, when you give template parameter inside <SomeType> following vector, C++ compiler generated code for a class of type SomeType. So when you populate the vector using push_back, you are actually inserting another object of SomeType so when you request .size() from the compiler it gives you the number of SomeType objects inserted by you.
Hope that helps!

Rajendra Kumar Uppal
Well that's an around-the-barn answer... Who said he's using `push_back` and how is that relevant? How is the fact that vector is a template relevant? But at least you got the point that `size()` is what the OP is looking for.
@STingRaySCI was trying to explain to OP that sizeof has nothing to do with what he(/she) wanted. It is the .size() method that he(/she) should be looking for.
Rajendra Kumar Uppal