60

I am a bit confused about this both of these look same to me. Although it may happen that capacity and size may differ on different compilers. how it may differ. Its also said that if we are out of memory the capacity changes.

All these things are bit unclear to me.

Can somebody give an explanation.(if possible with and example or if I can do any test on any program to understand it)

munish
  • 4,505
  • 14
  • 53
  • 83
  • Also check out this answer http://stackoverflow.com/questions/2787397/whats-the-purpose-of-vector-capacity#answer-2787405. The language there is Java, but the concept is the same. – rid Jun 09 '11 at 17:39

9 Answers9

96

Size is not allowed to differ between multiple compilers. The size of a vector is the number of elements that it contains, which is directly controlled by how many elements you put into the vector.

Capacity is the amount of total space that the vector has. Under the hood, a vector just uses an array. The capacity of the vector is the size of that array. This is always equal to or larger than the size. The difference between them is the number of elements that you can add to the vector before the array under the hood needs to be reallocated.

You should almost never care about the capacity. It exists to let people with very specific performance and memory constraints do exactly what they want.

Tobi
  • 2,591
  • 15
  • 34
John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • 3
    +1 clear to understand for me and also extra imp. info-->"You should almost never care about the capacity. It exists to let people with very specific performance and memory constraints do exactly what they want." Thanks – munish Jun 09 '11 at 17:54
  • 15
    Disagree about not caring about capacity. If you know you have a minimum 200 items to be stored in your vector, you'd be crazy not to tell it that whilst constructing. – Kent Boogaart Jun 09 '11 at 18:00
  • 7
    Crazy? I don't necessarily think so. I just ran a few tests, inserting 200 `int`s with and without reserving space, and I needed over 100,000 repetitions before reserving space showed a significant performance improvement. Granted, the performance was about twice as good when space was reserved, but unless you're doing this in a tight loop and this is your performance bottleneck, I don't really think it's crazy to not care very much about a couple of microseconds. – John Calsbeek Jun 09 '11 at 18:36
  • 6
    @John: so even with the knowledge that a re-allocation would be likely you'd still not pass on a single known number to the constructor in order to prevent said re-allocation? I stand by my "crazy". Even with ints on a modern machine (best case scenario test) I'd do it to make the intentions of the code clearer, never mind larger structs on an embedded system. – Kent Boogaart Jun 09 '11 at 19:25
  • 4
    @Kent: I don't disagree with the principle. I think reserving capacity is good practice if you're in the very specific situation where you have definitive knowledge about your container's end use. That is why the "never" in my answer reads "*almost* never". – John Calsbeek Jun 09 '11 at 19:36
  • 1
    The number of allocations is going to be logarithmic relative to the number of elements. This is almost always negligible. It does not even matter how many elements the vector will contain. – Don Reba Jun 09 '11 at 19:49
  • 3
    @Don: Indeed. But the total number of item copies is *O(N)*. – Oliver Charlesworth Jun 09 '11 at 23:38
  • 1
    Note that there are more reasons to care about capacity than performance or "good practices". You might also care about iterator invalidation: if you `reserve` N elements and pass a pointer to `&vec[0]`, then you can insert up to N elements without invalidating that pointer. – KABoissonneault Nov 23 '16 at 18:32
  • Is the 'capacity' any different from 'max_size' ? – Ayush Apr 26 '21 at 11:05
  • 1
    @Ayush `max_size` is the largest that `size` can get on that platform. Often that is only a theoretical limit, because no one has enough RAM in one machine to fill 64 bits of address space – Caleth May 25 '21 at 10:14
41

Size: the number of items currently in the vector

Capacity: how many items can be fit in the vector before it is "full". Once full, adding new items will result in a new, larger block of memory being allocated and the existing items being copied to it

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • 1
    I'd say not allocate but re-allocate. Because vector guarantees that data is continuously laid out in memory, it cannot use more than one memory blocks returned by operator `new`. So when the limit is hit, vector will allocate a new chunk of memory and copy existing data to that memory, then delete the previously allocated block. Or, if it is smart enough, it will use `realloc` function to be a bit more optimal. –  Jun 09 '11 at 17:41
  • @Vlad: I'm assuming you commented before seeing my last edit? – Kent Boogaart Jun 09 '11 at 17:58
  • thanks this answer helped me understand it easier than the accepted answer. I've created this pastebin for this answer for future visitors: https://pastebin.com/Xx1RAc9K , also the example code here http://www.cplusplus.com/reference/vector/vector/reserve/ is much clearer now. – aderchox Dec 17 '19 at 09:38
29

Let's say you have a bucket. At most, this bucket can hold 5 gallons of water, so its capacity is 5 gallons. It may have any amount of water between 0 and 5, inclusive. The amount of water currently in the bucket is, in vector terms, its size. So if this bucket is half filled, it has a size of 2.5 gallons.

If you try to add more water to a bucket and it would overflow, you need to find a bigger bucket. So you get a bucket with a larger capacity and dump the old bucket's contents into the new one, then add the new water.

Capacity: Maximum amount of stuff the Vector/bucket can hold. Size: Amount of stuff currently in the Vector/bucket.

Coeffect
  • 8,772
  • 2
  • 27
  • 42
8

Size is number of elements present in a vector

Capacity is the amount of space that the vector is currently using.

Let's understand it with a very simple example:

using namespace std;

int main(){
  vector<int > vec;
  vec.push_back(1); 
  vec.push_back(1); 
  vec.push_back(1); 
  cout<<"size of vector"<<vec.size()<<endl;
  cout<<"capacity of vector"<<vec.capacity()<<endl;
  return 0;
}

currently size is 3 and capacity is 4.

Now if we push back one more element,

using namespace std;
  int main(){
  vector<int> vec;
  vec.push_back(1); 
  vec.push_back(1); 
  vec.push_back(1); 
  vec.push_back(1);
  cout<<"size of vector"<<vec.size()<<endl;
  cout<<"capacity of vector"<<vec.capacity()<<endl;
  return 0;
}

now size is: 4 capacity is 4

now if we try to insert one more element in vector then size will become 5 but capacity will become 8.

it happens based on the datatype of vector, as here in this case vector in of type int, as we know size of int is 4 bytes so compiler will allocate 4 block of memory ..and when we try to add 5th element , vector::capacity() is doubled what we have currently.

same keep on..for example : if we try to insert 9th element then size of vector will be 9 and capacity will b 16..

sanjeev
  • 590
  • 1
  • 11
  • 21
  • 1
    I think your explanation of why the capacity gets doubled is vague. The number returned by capacity is about the number of "elements" we can add before reallocation and not about bytes. capacity of a vector of integers with two numbers in it is 2, if this 2 were in bytes, then even a single integer could not fit in the vector. Maybe I'm missing a point. Can you explain why the capacity doubles more clearly? – aderchox Dec 17 '19 at 09:29
  • My problem is worse. MSVC is returning the same number for both irrespective of how many elements I add – kesarling He-Him Mar 27 '20 at 06:27
  • I was using MS VStudio 17 but size and capacity are same for me in all cases – Himanshu Poddar May 27 '21 at 14:03
  • @aderchox this will help https://stackoverflow.com/questions/54889521/whats-the-difference-between-len-and-capacity#answer-54889560 – Tony Montana May 13 '23 at 15:01
6

size() tells you how many elements you currently have. capacity() tells you how large the size can get before the vector needs to reallocate memory for itself.

Capacity is always greater than or equal to size. You cannot index beyond element # size()-1.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
5

The size is the number of elements in the vector. The capacity is the maximum number of elements the vector can currently hold.

Pete
  • 10,651
  • 9
  • 52
  • 74
0

The vector size is the total number of elements of a vector and it is always the same for all compilers. Vectors are re-sizeable.

The capacity is the maximum number of elements the vector can currently hold. It may differ for different compilers.

Capacity changes if it needs to, or you can set an initial capacity and it will not resize until that capacity is reached. It is automatically expanded.

Capacity > = Size

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
0

One is more of an important interface and the other is more of an important implementation detail. You will mostly deal with size and not capacity. In other words:

  • Size is the number of items in the vector. If you want to iterate through the vector, you need to know its size.

  • Capacity is how many items can be fit in the vector before more memory must be allocated to it. Once the capacity limit is reached, more memory is allocated to the vector.

An analogy to size is the number of balls in a box whereas the capacity is the box size. When programming, you normally want to know how many balls are in the box. The vector implementation should handle the capacity for you (making a bigger box once it is full).

enter image description here

Dean P
  • 1,841
  • 23
  • 23
0

To give a more visual answer to what have been described above. Try to see the change in memory location whenever the vector's capacity changes.

Memory location changes of the vector

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34789928) – Nol4635 Aug 08 '23 at 23:59