What is vector in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

What is vector in C++?

Vector in C++?

20th Feb 2018, 2:11 PM
Adarsh Singh
Adarsh Singh - avatar
3 Answers
+ 9
http://en.cppreference.com/w/cpp/container/vector http://www.cplusplus.com/reference/vector/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 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.
20th Feb 2018, 2:14 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 16
Just refer here:-) https://www.sololearn.com/learn/261/?ref=app C++ has a vector class within the std namespace. A vector is similar to an array, in a sense where a series of elements are stored with the same variable name. 
20th Feb 2018, 2:18 PM
Abhivarshini Maddala
Abhivarshini Maddala - avatar
+ 10
Thanks!
20th Feb 2018, 2:16 PM
Adarsh Singh
Adarsh Singh - avatar