What are vectors in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What are vectors in C++

14th Nov 2017, 6:45 AM
Paavan Gupta
Paavan Gupta - avatar
3 Answers
+ 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.
14th Nov 2017, 6:50 AM
MsJ
MsJ - avatar
+ 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/
14th Nov 2017, 6:52 AM
Cain Eviatar
Cain Eviatar - avatar
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.
14th Nov 2017, 7:14 AM
Yutaka Nagahata
Yutaka Nagahata - avatar