size vs capacity of a vector?

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)

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 space that the vector is currently using. 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.

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

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.

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.

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

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