I am doing the codecademy c++ course and just went over vectors and arrays. From what I understand, the only difference between the two is that you can change the size of a vector, but cannot change the size of an array. Are there any other differences that I am missing that would make them more useful in the future?
Asked
Active
Viewed 71 times
-1
-
*the only difference between the two is that you can change the size of a vector,* -- Vectors do not decay to pointers when passed to functions. – PaulMcKenzie Apr 19 '20 at 23:57
-
@PaulMcKenzie neither does `std::array`. But both containers do have a `data()` method for getting such a pointer – Remy Lebeau Apr 20 '20 at 00:45
-
I was assuming the author was referring to regular arrays, not std::array. – PaulMcKenzie Apr 20 '20 at 00:49
1 Answers
2
A major difference is that std::vector
allocates memory, i.e. it uses the heap for storage. An array (std::array
or a built-in one) does not.
Allocating memory is a major difference because it is a very expensive operation relative to many others (when using the default allocator).

Acorn
- 24,970
- 5
- 40
- 69