A Guide to C++ Vectors

You can access the full course here: C++ Foundations

Vectors

Arrays aren’t great when we need a list of data that will change in size, i.e. we will add elements to it or remove elements from it. For that, we can use a vector. It is a more functional version of an array that still stores data in a list-like format but can grow or shrink. We have to first import the vector library by adding this line of code at the top:

#include <vector>

Then we can replace our roster with this:

std::vector<std::string> roster;

Not how we don’t initialize it with items initially and we set the type of values in the vector to string in the <>. We can add them with the .push_back() function like this:

roster.push_back(“Nimish”);
roster.push_back(“Sally”);
roster.push_back(“Laura”);

We can insert a value by specifying the index and the new value using the insert() function like this:

roster.insert(roster.begin() + 1, “Mike”);

This weirdly enough, needs an indexing type variable rather than just a regular int index so note the roster.begin() (start index) + 1 to insert it after the first element. We can remove an element from the back by using the pop() function like this:

roster.pop_back();

There are other functions to explore so check them out! You can access them by typing

roster.

And then a list of possible functions should pop up.

 

Transcript

What’s up guys? Welcome to our tutorial on vectors. Here we’ll take a look at a collection type that is similar to an array but a little bit more powerful. So we are going to first learn how to create vectors, then how to add elements to vectors, how to remove elements from vectors. Let’s head to the code and get started.

So let’s begin with talking about what vectors are. Well vectors really are just more powerful versions of arrays. So these are arrays that are mutable, meaning we can add elements to them and remove elements from them, but they also have a host of other functions that allow us to either manipulate them in some way or to retrieve properties of them. So really where we have static data that we know isn’t going to change very much, that’s probably where we should use an array. As soon as we know that data is going to change, it’s going to grow or shrink in size, then we probably need to turn to something like a vector.

Alright to gain access to the vector we actually have to add another include statement. So we need to include the vector library like so. Now these are part of the standard library so we’re going to need to do a standard vector like so, and then in the angular brackets beside it, we put the type of variable that we want to store in here. So let’s do basically the same thing as before with the roster, might as well keep the same examples. So we’ll do standard string here. It’s going to be a vector of strings. And then we just need to give this a name so in this case maybe roster or something.

Now we’re not going to set this up by providing initial values or anything. What we’ll do instead is create it and then we’ll just push items onto it. So when we push something onto a vector it just sticks it right onto the end. So in this case we’ll do roster and we’ll call the push back function, and then we just need to parse in an appropriate value. So in this case I’ll just maybe do my own name first. Okay and then maybe we’ll add a couple more people So let’s do Sally, and we’ll do Laura or something. Okay, just some people that we might want to add to our roster. So now it’s three members long, it starts off with my name and then the next and then the next.

Okay, so each one you add just goes to the back of the stack. Similarly we can remove elements from a vector by doing the roster in this case, or the vector name dot pop back. So the pop back function is just going to remove the last element in a vector. We can keep doing this until we have popped all the elements or until we get to the one we want.

Alternatively if I want to insert an item at a specific index, lets say I want to put someone right after myself but before Sally, what they can do is they can do roster dot insert. And with the insert function this takes first the position then the value. Now annoyingly enough I actually can’t just do the position of one, what I would have to do is something like roster dot begin plus one. And then my value, lets do Mike or something. Okay, so this is now going to insert the value of Mike in between me and Sally because it gets the beginning, or zero, plus one which would be this index and it just inserts it right here, so basically pushes this back and puts that new value Mike right there.

Now these are just a few of the many possible operations we could perform on vectors. You can open them up, see a complete list by doing the name dot and now you can see that there are a lot of different functions that we can call on these vectors. So feel free to check some of them out, otherwise we covered the basic operations of add remove and insert. Definitely give it a bit more of a play around. When we come back we will be talking about if statements and that will be our first introduction into conditional logic in our code. So stay tuned for that, thanks for watching, see you guys in the next one.

 

Interested in continuing? Check out the full C++ Foundations course, which is part of our One-Hour Coder Academy.