Vectors Explained With 20 Functions 28mar24
Vectors Explained With 20 Functions 28mar24
Vectors are similar to arrays but with dynamic sizing. They can grow and shrink in
size automatically as elements are added or removed. Vectors guarantee contiguous
storage, meaning that elements are stored in a single, contiguous block of memory,
allowing for efficient element access using iterators or indices.
1. Dynamic Sizing:
Vectors can grow or shrink in size automatically as elements are added or removed.
This dynamic resizing is handled internally by the vector container.
2. Contiguous Memory:
Elements in a vector are stored in contiguous memory locations, allowing for
efficient access using pointers or iterators.
3. Random Access:
Elements in a vector can be accessed randomly using indexing or iterators. This
allows for efficient access to any element in the vector.
Overall, vectors are a versatile and commonly used data structure in C++ for
storing and manipulating sequences of elements, offering a good balance between
flexibility, efficiency, and ease of use.
================================================
2. TWO main Advantages & Disadvantages of Vectors:
================================================
Advantages:
-----------
1. Dynamic Sizing: Vectors automatically manage memory allocation and resizing,
allowing them to grow or shrink as needed. This dynamic sizing makes vectors
flexible and convenient for storing varying numbers of elements without manual
memory management.
2. Random Access: Vectors provide efficient random access to elements, both through
indexing and iterator-based traversal. This allows for constant-time access to any
element in the vector, making it suitable for scenarios where fast access to
elements is required.
Disadvantages:
---------------
1. Contiguous Memory: Vectors store elements in contiguous memory, which means that
inserting or deleting elements in the middle of the vector can be inefficient. When
elements are inserted or removed from the middle of a vector, all subsequent
elements must be shifted, resulting in potentially costly operations, especially
for large vectors.
2. Memory Overhead: Vectors may have a higher memory overhead compared to some
other data structures, particularly when they are not fully utilized. Vectors
reserve extra memory to accommodate potential growth, which can lead to wasted
memory if the actual number of elements is much smaller than the reserved capacity.
This overhead can be mitigated by careful management of vector capacity or by using
alternative data structures like lists when memory efficiency is critical.
=====================
3. Vector Functions:
=====================
3. pop_back()
Removes the last element from the vector.
5. erase(iterator pos)
Removes an element at a specific position using an iterator.
7. size()
Returns the current size (number of elements) of the vector.
8. empty()
Returns true if the vector is empty and false if it contains elements.
9. clear()
Removes all elements from the vector, making it empty.
10. front()
Accesses the first element of the vector.
11. back()
Accesses the last element of the vector.
12. at(size_type n)
Accesses the element at a specific index.
14. reserve(size_type n)
Allocates memory for a specified number of elements without changing the size of
the vector.
15. capacity()
Returns the current capacity (maximum number of elements that can be stored) of the
vector.
16. shrink_to_fit()
Reduces the capacity to match the size of the vector.
17. resize(size_type n)
Changes the size of the vector. New elements are default-initialized.
21. swap(vector& x)
Swaps the contents of two vectors.