Here’s a quick tutorial on using the C++ vector from the Standard Template Library (STL). Be sure to check my tutorial on C++ maps, also from the STL. The Standard Template Library (STL) is a collection of data containers and ways to manipulate the data we put in those containers. It is a way to store and manipulate different types of C++ variables and classes. We can think of the different containers as way more advanced arrays. The STL is part of C++ because its containers and the code that manipulates them contain code that we and just about every C++ programmer are all going to need at some point.

patreon

 

 

To begin, let’s include the necessary header file for using vectors:


Next, let’s declare and initialize a vector. We’ll start with simple examples using int and string types:


In the first line, we declare an empty vector of integers called numbers. In the second line, we declare a vector of strings called names and initialize it with three values: “Alice”, “Bob”, and “Charlie”.

Now, let’s add elements to the vector using the push_back function:


The push_back function appends elements to the end of the vector. In the first line, we add the integer value 10 to the numbers vector, and in the second line, we add the string value “Dave” to the names vector.

To access elements in the vector, we can use the subscript operator [] or the at() function:


In the first line, we retrieve the first element of the numbers vector using the subscript operator. In the second line, we retrieve the second element of the names vector using the at() function, which performs bounds checking to ensure we’re accessing a valid element.

If we want to modify elements in the vector, we can directly assign new values:


In the first line, we change the value of the first element in the numbers vector to 20. In the second line, we update the third element in the names vector to “Eve” using the at() function.

Looping through the vector is useful for iterating over its elements. Here are two examples of looping through the vector:


The first loop uses a traditional for loop with an index variable. We iterate from 0 to numbers.size() - 1 and access each element using the subscript operator []. The second loop uses a range-based for loop, which iterates over each element in the names vector directly, assigning the value to the name variable.

Now, let’s work with pointers to GameObject instances in the vector. We’ll use smart pointers to manage memory automatically:


In this example, we include the <memory> header to use smart pointers. We define a struct called GameObject to represent game objects. Then, we create a vector called gameObjects to store pointers to GameObject instances.

To add a new GameObject to the vector, we create a shared pointer using std::make_shared<GameObject>() and add it to the gameObjects vector using push_back.

Finally, we iterate over the gameObjects vector using a range-based for loop. Each element in the vector is accessed as a const std::shared_ptr<GameObject>& reference, allowing us to access and manipulate the game objects as needed.

 

To expand upon this idea look at the tutorial Programming a simple game engine in C++.

patreon