0% found this document useful (0 votes)
8 views

Vectors Explained With 20 Functions 28mar24

A vector is a sequence container that dynamically manages storage space for its elements. Vectors provide efficient random access to elements and automatically manage memory allocation and resizing. Key vector functions include push_back, pop_back, insert, erase, and access functions like at and [].

Uploaded by

aoun.m.8255
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Vectors Explained With 20 Functions 28mar24

A vector is a sequence container that dynamically manages storage space for its elements. Vectors provide efficient random access to elements and automatically manage memory allocation and resizing. Key vector functions include push_back, pop_back, insert, erase, and access functions like at and [].

Uploaded by

aoun.m.8255
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

============================

1. What is a Vector in C++?


============================
In C++, a vector is a sequence container that dynamically manages storage space for
its elements. It is part of the Standard Template Library (STL) and provides a
flexible and efficient way to store and manipulate sequences of elements.

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.

Here are some key features of vectors in C++:

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.

4. Efficient Insertion and Deletion:


Vectors support efficient insertion and deletion operations at the end of the
vector (`push_back`, `pop_back`), as well as at any other position (`insert`,
`erase`), although inserting or deleting elements in the middle of the vector can
be less efficient compared to other data structures like lists.

5. Standard Library Algorithms:


Vectors can be easily manipulated using a wide range of algorithms provided by the
C++ Standard Library, such as sorting, searching, and modifying algorithms.

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:
=====================

How to read functions given below:


----------------------------------
In the functions below, parameters are written usnig the terminology (const T&
value). It means that the function takes a constant reference to an object of type
T as an argument. For example, if we have a vector<int>, T would be replaced with
int. Thus value is passed as 'const' and a reference to the value is passed as
shown by '&'. This means that value does not hold the actual object itself but
rather refers to an existing object elsewhere in memory. Using a reference allows
passing large objects efficiently without incurring the overhead of copying.

1. push_back(const T& value)


Adds an element to the end of the vector. When the vector needs to grow and there's
insufficient capacity, it reallocates memory and copies/moves the existing elements
to the new memory location.

2. emplace_back(const T& value)


Constructs an element in place at the end of the vector without unnecessary
copying/moving. It directly constructs the element in the allocated storage,
potentially avoiding the overhead of copying or moving existing elements.
Therefore, emplace_back may result in fewer reallocations and potentially better
performance compared to push_back.

3. pop_back()
Removes the last element from the vector.

4. insert(iterator pos, const T& value)


Inserts an element at a specific position using an iterator.

5. erase(iterator pos)
Removes an element at a specific position using an iterator.

6. erase(iterator first, iterator last)


Removes a range of elements specified by two iterators.

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.

13. operator[] (size_type n)


Accesses the element at a specific index using the subscript operator.

Example => myVector[i] = 25;

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.

18. resize(size_type n, const T& value)


Changes the size of the vector and initializes new elements with a specified value.

19. assign(InputIterator first, InputIterator last)


Assigns elements from a range defined by two iterators.

20. assign(size_type n, const T& value)


Assigns a specified number of elements, each initialized with a given value.

21. swap(vector& x)
Swaps the contents of two vectors.

Example => myVector.swap(anotherVector);

You might also like