Adding and removing elements
One of the advantages of std::vector over traditional arrays is its ability to resize dynamically. As applications evolve, so do data requirements; static data structures do not cut it. In this section, we will explore dynamic data management with std::vector, learning to seamlessly add to and remove from vectors while making sure we are staying safe.
Adding elements
Let’s start with adding elements. The push_back() member function is possibly the most straightforward way to add an element to the end of a vector. Suppose you have std::vector<int> scores; and wish to append a new score, say 95. You would simply invoke scores.push_back(95);, and voilà, your score is added.
Here’s a simple illustrative code example:
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores;
std::cout << "Initial size of scores: " << scores.size()
...