size vs capacity of a vector?

后端 未结 6 1621
刺人心
刺人心 2020-11-27 03:10

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 sa

相关标签:
6条回答
  • 2020-11-27 03:35

    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.

    0 讨论(0)
  • 2020-11-27 03:40

    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.

    0 讨论(0)
  • 2020-11-27 03:46

    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.

    0 讨论(0)
  • 2020-11-27 03:47

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

    0 讨论(0)
  • 2020-11-27 03:56

    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

    0 讨论(0)
  • 2020-11-27 03:58

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

    0 讨论(0)
提交回复
热议问题