What is a vector?

What are they even used for? I see people using them all the time in this forum and I could never really figure out what it is. Any help?
It is a template container used to store data.
http://www.cplusplus.com/reference/stl/
http://www.cplusplus.com/reference/vector/vector/

class template
<vector>
std::vector
template < class T, class Alloc = allocator<T> > class vector; // generic template
Vector
Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Compared to the other dynamic sequence containers (deques, lists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.
closed account (EwCjE3v7)
You would usually use an array if you new the amount of elements you needed otherwise you would use a vector and the pushback function
To me, it seems like vectors aren't really a huge thing for programming... am I wrong?
STL containers are pretty important. Unless you wish to dynamically allocate everything manually. Also it makes iterating over the containers very easy and there are a bunch of neat algorithms you can use to save time.
http://www.cplusplus.com/reference/algorithm/

http://www.learncpp.com/cpp-programming/16-2-stl-containers-overview/
Oh, now it makes sense. I should probably learn how to use them now...
Topic archived. No new replies allowed.