I am usually unsure when it is better to use one versus the other. They both seem to do the same things in general but is vector more flexible in terms of what it can do? When are arrays more appropriate?
-
1Almost never use arrays is my suggestion. But, by array do you mean `type []` or `std::array`? – Richard J. Ross III Jun 02 '12 at 20:53
-
The main difference between a vector and an array, in a C++ sense, is that vectors do automatic dynamic memory management, whereas arrays are fixed. – chris Jun 02 '12 at 21:04
2 Answers
Generally always prefer using std::vector<T>
since the destruction will be automatic once the vector goes out scope and the allocated memory will be placed neatly on the heap and all the memory will be handled for you. std::vector<T>
gives you everything you get in an array and even a guarantee that the elements will be contiguously stored in memory (except for std::vector<bool>
).
In the case of std::vector<bool>
you have to be careful since code like this will break:
std::vector<bool> vb;
vb.push_back(true);
vb.push_back(false);
vb.push_back(true);
bool *pB = &vb[0];
if( *(pB+1) )
{
// do something
}
Fact is, std::vector<bool>
doesn't store contiguous bool
s. This is an exception in the standard that is fixed in C++11.

- 6,817
- 2
- 25
- 43
-
2
-
+1 for *will be placed **neatly** on the heap* (emphasis mine), which means that accidentally writing outside the array won't smash the stack. – Matthieu M. Jun 02 '12 at 23:32
I only really use arrays when working with embedded systems, or when certain APIs require it (ie take them in as an argument for a function). Also, if there are only one or two places where I'd need to use arrays, or don't specifically need vector functionality, it makes more sense to use arrays just because of the additional overhead of vector.h.
Otherwise, just use vectors :)

- 731
- 8
- 26
-
3You can pass vectors in when it's expecting an array: `someFunc (&v[0]);`. – chris Jun 02 '12 at 21:01
-
Your other point is valid though. Depending on how and how often you're using the array, as well as your program's needs, the overhead of a vector can be excessive. – chris Jun 02 '12 at 21:07
-
2@chris What overhead are you talking about? A vector stores an additional integer for the capacity, that is all. And in reality, you will need this for most applications anyway. And the overhead of including the header is negligible, and in most cases it’s present anyway when you write modern C++ (certain embedded platforms excluded). – Konrad Rudolph Jun 02 '12 at 21:27
-
@KonradRudolph, I'm just referencing the rare occurrence when the overhead of every time you use a functionality of the vector (another function call) matters. Of course 1 program in a million cares, and I may be behind the times with what compilers can do. – chris Jun 03 '12 at 02:20
-
2@chris Ah, then you are mistaken. A vector has **no** function call overhead. In fact, performance-wise, it is completely equivalent to using a C array. I’m actually the one programmer in a million (have worked on a bioinformatics library) so we *did* measure this rather carefully. – Konrad Rudolph Jun 03 '12 at 09:32
-
@KonradRudolph, Very good to know, thanks. That just makes vectors all the better. – chris Jun 03 '12 at 12:40
-
@chris are you sure that is guaranteed that someFunc(&v[0)) will work? I mean, thinking about the implementation it will work if the underlying stdc++ vector implementation declares the pointer to the data as first member of the class. if for some reason the template declares the pointer to the start of the data as second member it just won't work. – crsnplusplus Dec 11 '14 at 11:17
-
2@crsn, It doesn't matter what order the class's members are in. Assuming an element exists, `v[0]` is required to give you a pointer to the first element of a contiguous sequence of elements (like the same sort of pointer returned by `new int[5]`). – chris Dec 12 '14 at 22:01