The Vector Class


Course Overview
Arrays and Vectors
Built-in Arrays
PREVIEW18m 31s

The course is part of this learning path

Start course
1h 25m

This course explores arrays and vectors which are types of data structures in C++. We'll focus on built-in arrays, which C++ inherits from the C programming language, and a couple of sequence containers including the array template class and the vector template class, which are part of the standard template library or STL.

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • College students and anyone studying C++


To get the most out of this course, you should have a basic understanding of the fundamentals of C++.



In this lecture, we will discuss the vector class from which you can create objects that can hold elements just like arrays do except that vector objects are not limited to a fixed size. Instead, vectors can resize as more elements are added to them. Because of this, you don't have to specify an initial capacity although you can. Let's create a project named VectorFun. So, we create a new project, Empty project and we will call it VectorFun. Let's create a new source file here. main and we will fill in our skeleton code and we also know that we will need vector. So, using namespace std;. Let's also add string because I think we're going to make a vector of strings. Now, we'll declare a vector. We'll discuss this in a second. We'll have one vector of integers, one of strings. So, I'll call this just someVec and then one of strings, anotherVec and then this one actually has an initial size. We need to make sure that that is closed. There we go. And here's how I'm going to add some elements. someVec.push_back(1);. This will add them to the end. someVec.push_back(2); and someVec.push_back(3);. Notice I'm doing this to the first vector here that I did not declare a size for so it is dynamically resizing as needed. I'm going to print out the size of the vector. So, "someVec size: "<< someVec.size() <<endl;. This is the size method. Tells you how many elements are in the vector. And then for anotherVec. Notice this is going to be a little bit different. This is a vector of strings. Of course, that's one thing that's different but the major thing you'll notice is that I'm using the array index operator, in this case, the subscript operator for vectors. There we go. And then I'm going to do this, push_back ("Shannon");. And let's do some printing then we'll discuss this a little bit better, little bit more. For (int val : someVec), we're going to have count << val  << endl;. And then maybe some space just to provide space between the two and then the names. So, each name in anotherVec. We're going to print out that name followed by int l, and then we have the return zero at the bottom. Now, let's run this and see what happens. I built it first. It doesn't really matter as long as there's no errors. If there's errors, it will tell you it won't build anyway. So, start without debugging and we have someVec size is three which it should be printing that out, correct. So, it will print out the size before it does any of the actual elements. So, print out the size. That's the very first thing we should see. Then, it goes through the iteration here, prints out each of the values in someVec one, two, and three, and then four, prints out a new line character which is where that emptiness right here comes from. That's the new line. Then, it prints John, Bob, Sally but also Shannon, which we added with push_back. So, in our example, we have these two vectors. One of the vectors someVec is a vector that we do not specify an initial size four. We use the push_back member function or method to add elements to the end of the vector and the vector handles allocating the memory that we need in order to store these new elements. So, it is resizing as needed to add the different integers. Our other vector, anotherVec is a vector of strings. In this case, we have initialized the size of the vector to three because we have that right here. But we have to resize it only after we go past three elements. So, notice that we do not use push_back to add or replace elements in the cells that already exist. We have three cells that already exist because we declare it with an initial size. We just use them as we would any regular array or built in array. Then, when we go beyond the initial capacity, we have to use push_back because it creates an additional cell. Ultimately, it creates room for it and also places our element in there. So, vectors are just like arrays but will automatically resize as needed. So, we're going to look at five-member functions also called Methods that we haven't seen yet. They are Front, which returns the front element, Back, which returns the back element, Pop_back, which removes the element at the end of the vector and Insert, which allows us to insert an element given a specific location within the vector. The location to insert has to be an iterated though so we don't just use simple indices like we would with a regular built-in array. We will use the method begin which returns the beginning of the vector so we can observe how to insert at the beginning of the vector. And we can also use integers to offset that as well. So, let's see what happens when we add those. So, I'm going to close this right here and right here we're going to right below all of this stuff, I'm going to say cout << "Front and back:"<< endl;. And we're going to say "front : "<< anotherVec.front() so that actually returns element at the front. The back, we can say anotherVec.back() << endl;. I'll return the element at the end. Now, let's see what happens if we say anotherVec.pop_back(); and then anotherVec.insert(). We're going to insert at a specific location which will be a specific index within the vector. And the insert method actually takes two arguments. So, we have (anotherVec.begin() that says, where do I insert? And then you put the element that we insert, "Don");. Now, I'm going to print out. Now, front is anotherVec.front() << endl;. Fix that. There we go. And we're going to say "now, back is "<< anotherVec.back()<< endl;. And we have return zero at the bottom. I don't see any obvious errors so I think we're good. So, let's run this and see what happens. There we go. We have our initial stuff that we printed, front and back. Front is John which should be what it shows and it is and then back as Shannon which is what that should be as well. And you'll notice what we did in between was we said pop_back which would have removed Shannon from the end and then we inserted at the front the name Don. So, you notice now it says front is Don, back is Sally. So Shannon was eliminated so Sally is the new back. And John was at the front but since we said, "Hey, I want to insert at the beginning of this vector the name Don, Don would actually become the new name." So, it would actually be Don, John, Bob, Sally, and then Shannon is gone. So, that's why it says Don and Sally here. So, notice we can observe the front and the back easily with these convenient member functions also called Convenience Methods. Meaning there are other ways of getting the front and the back using indices, for example, but they're here for our convenience. Note also that when we use pop_back, it removes the item from the end or back of the vector. Just like push_back adds items to the back, pop_back removes items from the back. We also observed the power of the insert and begin methods. To review, the insert method takes two arguments. Right here and there. The first is an iterator to the location to do the insertion and the second argument is the element to add. Notice that we can use addition with the begin method to indicate a position relative to the beginning. For example, if you use +2 right here in the begin, if you use  +2 after the begin method, it would insert the string Don into position two which is the third element since member we're indexing from zero. So, instead of putting it at the beginning, it would be the beginning +2 so the third element. Before we move on to the next lecture, you guessed it. I have a challenge for you. I want you to create a project called VectorPractice and I want you to first add the names of five friends, family members, pets or celebrities, whoever you want, to your vector. Then, I would like you to insert my name, John Baugh, as the third element in the vector. Once you've done that, remove the last element from the vector and then print all the elements remaining in the vector. So, pause the video and give it your best shot. Come back when you're done. Did you get it? Let's do it together so you can see the solution I come up with. So, we're going to close this right here, this project. Close this solution and we're going to create a project called VectorPractice. So, VectorPractice. Hit Create. Create our file here, new item. We're going to put main.cpp there, include < iostream >. Since it's going to involve names, I need the string and since it involves vector, I need the vector library. So, string and vector libraries using namespace std;. We have our skeleton program here. So, we want to add five names to our vector of friends, family members, pets or celebrities, whoever we want. I'm not going to add myself because that's brought in later. So, the first step was creating the vector. So, vector <string>, we'll call it names;. And I could initialize it to five if I wanted to and that's fine if you did that and then use the indexing operator. That's totally fine. I'm going to use push_back so that will add all the elements to the end each time. So, let's do this. names.push_back. We're going to say Jackson push_back Hoppy is my cat, one of my cats. Orange is my other cat, push_back Rozie is my brother and sister in law's and niece's dog, and then push_back("Philip"). That's my nephew who lives in Colorado. So, we've got my brother's name, two pets, my brother and sister in law's dog, and my nephew. So, those are the five names. Then it says, I want you to insert the name John Baugh, my name, as the third element in the vector. So, we can't use push_back or that will add it to the end. But you remember we have the Insert Method, the insert member function, names.insert() and it takes two parameters. So, anotherVec. Not anotherVec, sorry. (names.begin() ). We want it to be the third element, so that's the element with index 2. So, we've got zero is at begin. It would be one if it was +1 and two since it's +2. And we want to add the name John Baugh there. Now, I could fill in the last names and things like that here but I'm just leaving it. The other thing it says to do is once we've done this is to remove the last element from the vector. So, I'm going to say names.. How do we remove the last element? So, pop_back. That's right. So, we insert using push_back so we can remove it using pop_back. And then it says to print all the elements remaining in the vector. And we know how to do this. There's a couple different ways but we'll use the enhanced or range based fore loop. For(string name : names) and we're going to print each of those names. We'll give up their own line. We don't have to but we will. Let's run this and see what we get. So, we get Jackson, Hoppy, John Baugh was added as the third element. That's what we wanted. Orange and Rozie, where'd Philip go? Well, sorry nephew but you were popped off of this vector. So, you got eliminated. So, I hope that you were able to get that and hopefully, that was fun for you. So, are you getting a hang of it? I hope you're getting the hang of it. You can always ask questions if you get stuck. I sure hope you're starting to pick up some of these skills and that you can see how powerful some of the skills can be. In the next lecture, we will go back to built-in arrays but look at multidimensional arrays with a focus on two dimensional arrays. I'll see you in just a bit.


About the Author
Learning Paths

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics