## C++ Program to Find the Size of the Vector

Now look at a proper C++ program to understand this properly. This program will not only retrieve the size of a vector but also show how the size changes as we add and remove elements. This is crucial for applications where dynamic data structures are used to handle varying amounts of data.

### C++

`#include <iostream>`

#include <vector>

int main() {

// Creating a vector to store integers

std::vector<int> vec;

// Adding elements to the vector

vec.push_back(5);

vec.push_back(10);

vec.push_back(15);

// Printing the size after adding elements

std::cout << "Initial size of the vector: " << vec.size() << std::endl;

// Removing an element from the vector

vec.pop_back();

// Printing the size after removing an element

std::cout << "Size of the vector after one element is removed: " << vec.size() << std::endl;

return 0;

}

You can also try this code with Online C++ Compiler

Run Code
Output

```
Initial size of the vector: 3
Size of the vector after one element is removed: 2
```

### In this program

- We start with an empty vector of integers.

- We add three integers to the vector using push_back(), which increases the size of the vector each time an element is added.

- After adding the elements, we check the size of the vector using vec.size(), which should now return 3.

- We then remove the last element from the vector using pop_back(), which decreases the size of the vector.

- After removing the element, we check the size again, which should now be 2.

Note -: This example is essential for understanding how vector sizes adjust dynamically, which helps in writing programs that need to adjust to the changing data sizes efficiently.

## Time & Space Complexity

When working with vectors in C++, it's important to consider the time and space complexity of operations like adding, removing, and accessing elements. This understanding helps in writing efficient code, especially when dealing with large amounts of data.

### Time Complexity

**Accessing an Element:** Accessing an element by its index in a vector is a constant time operation, O(1). This means it takes the same amount of time regardless of the size of the vector.

**Adding an Element:** Adding an element to the end of the vector (using push_back()) also has a time complexity of O(1), generally. However, if the addition of an element exceeds the current capacity of the vector, the vector needs to increase its capacity, which involves allocating a new array and copying all elements to this new space. This operation has a time complexity of O(n), where n is the number of elements in the vector. But such cases are amortized and rare.

**Removing an Element: **Removing the last element of a vector using pop_back() is a constant time operation, O(1). However, removing elements from other positions can be more costly as it requires shifting the elements.

### Space Complexity

- The space complexity of a vector is O(n), where n is the number of elements it contains. Vectors manage their storage automatically and can grow dynamically. Although this provides flexibility, it requires careful management to avoid excessive memory use, especially in programs dealing with a large amount of data.

## Frequently Asked Questions

### What happens if I try to access an element out of the vector's range using the at() function?

If you attempt to access an element outside the vector's range using the at() function, it will throw an out_of_range exception. This is safer compared to using square brackets [], which do not perform any bounds checking.

### How can I reduce the memory overhead of a vector if I know it won't grow further?

To reduce the memory overhead of a vector after it has reached its required size, you can use the shrink_to_fit() method. This advises the vector to reduce its capacity to fit its size, potentially freeing up memory.

### Is it efficient to frequently add and remove elements from the beginning of a vector?

Adding or removing elements from the beginning of a vector is generally inefficient because each operation requires shifting all the subsequent elements. For operations that involve frequent insertions and deletions at the beginning, consider using std::deque or std::list which are more suited for these tasks.

## Conclusion

In this article, we have learned how to find and manage the size of a vector in C++. We explored practical examples of adding and removing elements, discussed their time and space complexity, and answered some common questions that arise when dealing with vectors.

You can refer to our __guided paths__ on the Coding Ninjas. You can check our course to learn more about __DSA__, __DBMS__, __Competitive Programming__, __Python__, __Java__, __JavaScript,__ etc. Also, check out some of the __Guided Paths__ on topics such as __Data Structure andAlgorithms__, __Competitive Programming__, __Operating Systems__, __Computer Networks,__ __DBMS__, __System Design__, etc., as well as some __Contests, ____Test Series__, and __Interview Experiences__ curated by top Industry.