1.

Advantages of a vector over an array in C++?

Answer»
  • Vector is resizable whereas Arrays are fixed. When the array reached its CAPACITY then the array won't extend implicitly but the vector implicitly extends its size by reallocating the elements by allocating more space.
  • Arrays have to be explicitly deallocated (if allocated dynamically) whereas vectors get automatically deallocated when it gets out of scope.
  • We NEED to manually keep track of the size of the dynamically allocated array explicitly whereas the size of the vector doesn't need to be TRACKED. This also means that we need to explicitly pass the size of the dynamically allocated array along with the array to a function (if we need to do some OPERATIONS in that function) but we don't need to pass the size of the vector to a function.
  • We cannot copy or assign the array to another array but we can copy or assign the vector to another vector easily.


Discussion

No Comment Found