0
What are vectors in C++
3 Réponses
+ 3
https://www.quora.com/What-is-std-vector-in-C++-and-when-is-it-useful
vector:
1)Contiguous memory
2).Pre-allocates space for future elements, so extra space required beyond what's necessary for the elements themselves.
3)Each element only requires the space for the element type itself (no extra pointers).
4)Can re-allocate memory for the entire vector any time that you add an element.
5)Insertions at the end are constant, amortized time, but insertions elsewhere are a costly O(n).
Erasures at the end of the vector are constant time, but for the rest it's O(n).
6)You can randomly access its elements.Iterators are invalidated if you add or remove elements to or from the vector.
7)You can easily get at the underlying array if you need an array of the elements.
+ 2
most of the time to use vector is when you need an array, but you don't know how much memory to allocate to him.
so basically a vector is some kind of dynamic array that adds and remove data, etc.
for instance:
vec.pushback(21) // size 1
vec.pushback(74) // added one more
and goes on...
check this out for more details. http://www.cplusplus.com/reference/vector/vector/
0
To understand what is std::vector, you'd better to understand it's advantage and disadvantage. But relative to what?
That would be all the other container types in std libraries. There are std::vector, std::array, std::list, std::map, std::unordered_map. They are strongly correlate with std algorithm too. Their computational complexity (approximately calcuration costs) are from their efficient data structures, so that how computer store your data change efficiency of your computation.