size vs capacity of a vector?

C++Vector

C++ Problem Overview


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)

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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

Solution 3 - C++

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.

Solution 4 - C++

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..

Solution 5 - C++

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.

Solution 6 - C++

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

Solution 7 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmunishView Question on Stackoverflow
Solution 1 - C++John CalsbeekView Answer on Stackoverflow
Solution 2 - C++Kent BoogaartView Answer on Stackoverflow
Solution 3 - C++CoeffectView Answer on Stackoverflow
Solution 4 - C++sanjeevView Answer on Stackoverflow
Solution 5 - C++Oliver CharlesworthView Answer on Stackoverflow
Solution 6 - C++PeteView Answer on Stackoverflow
Solution 7 - C++Talha TayyabView Answer on Stackoverflow