decque allows for efficient insertion on both ends (vector can efficiently insert only elements at end), but do not compatible with c-arrays. (for example you can pass pointer to the first element of the vector to the
qsort() function and it is guaranteed to work correctly, yo cannot do that with decque). It is actually specified
.begin() and
.end() too. As each STL container it is useful in certain situations. Often it is underlooked: I have seen peole which used
list only because it has fast insertion at the beginning.
I generally avoid c-arrays, unless they are stack allocated or fixed size class member ones, but even in those situation I prefer
std::array. Only exception: fixed-size char arrays as a buffer to read from binary file.
vectors are cool: they manages memory for you, prevents leaks, have almost none memory (+ 2 long long integers in my compiler) and CPU (subscript operator for vectors as fast as simple array access) overhead. Plus they have neat functions, knows their size and compatible with interfaces which expects c arrays.
.end() pointing past-the-end is a standard library requirement. It is like working with c arrays:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
//vector
const int size = 10;
std::vector<int> x(size);
for(auto it = x.begin(); x != x.end(); ++it)
//...
//Equivalent c-array
const int size = 10;
int* x = new int[size];
int* begin = x;
int* end = x + size;
for(auto it = begin; it != end; ++it)
//...
|