Vectors:
Are similar to arrays, but with the only difference being that it can change its size. Just like arrays vectors use contiguous storage locations for storing their elements as in the arrays. The size can be changed, however, unlike arrays. The storage is handled automatically by the container.
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, this can be expensive in terms of time, thus, to avoid these vectors do not reallocate the array. What they do is, vectors allocate some extra storage strictly need to contain its elements.
Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and also grow dynamically in an efficient way.
Vectors are, however, very efficient when accessing its elements, and also efficient when adding or removing elements from its end. But when inserting or removing at positions other than the end, they perform badly.
Some important member functions for vectors:
- begin: returns an iterator to the beginning of the vector.
- end: returns an iterator to the end of the vector.
- size: returns the size of the vector.
- resize: Change the size of the container.
- empty: Check whether the vector is empty or not.
- shrink_to_fit: Shrink the container to fit its contents.
- assign: Assigns new contents to the vector replacing its current contents.
- push_back: add an element at the end of the vector
- pop_back: Delete the last element of the vector
- insert: insert elements
- erase: erase elements
- swap: swap the contents with another vector
- clear: clear the content of the vector
Here is the link to the official documentation for vectors: http://www.cplusplus.com/reference/vector/vector/
No comments:
Post a Comment