In C ++, an array is basically just a pointer to a continuous block of data - a series of elements. It does not offer built-in methods or higher functionality.
int intArr[] = {0,1,2,3};
coincides with
int *intArr = (int *)malloc(4*sizeof(int)); for(int i = 0; i < 4; i++) { intArr[i] = i; }
On the other hand, a vector (std :: vector) is a container for elements (mainly as an array), which also offers additional built-in methods (see http://www.cplusplus.com/reference/stl/vector/ ) eg
vector<int> intArr; for(int i = 0; i < 4; i++) { intArr.push_back(i); }
Both arrays and vectors can be used for any type of object: int, double, MySpacePirateWizardClass, etc. The big bonus of vectors is additional functionality from built-in functions, such as:
int arrSize = intArr.size(); // vector also includes useful information int *firstElement = intArr.begin(); // methods for creating pointers to elements intArr.delete(0); // modify elements intArr.insert(0, 2); // modify vector // now: intArr = {2,1,2,3}
etc.
When I know that I will not miss space (or look at a huge amount of data), I always use vectors because they are so convenient (even just the size () method is sufficient reason).
source share