Explore vectors from a functional standpoint.
- [Instructor] Now let's get to know the vector class with a hands-on example. Here's the plan. Let's have four rounds of insertion. Let's first ask the user to insert elements on one end of the vector, the back, then to insert more elements on the other end of the vector, that's the front, then to insert elements in specific positions by index, and finally, to modify existing elements by index. In this demo, we'll use some number of functions of the vector class.
But be advised that you will need to refer to the standard library's documentation for details on syntax, parameter specification, iterator invalidation, and so on. Here we have some skeleton code where I've already taken care of the basics. I've included two header files I know I'll need for this example. The first is iostream, to use the cin and cout objects for a terminal interface and the second one is vector, which is where the SDL vector is implemented.
I've also added the line using namespace std, which allows me to stop specifying std in every item of the standard library, and this is a design decision. It's up to you to use it or not. After each round of insertions, I'd like to display the vector content. So for that, here we have a function named print that receives the vector as an argument. We'll come back to implement it later. Notice I'm declaring the parameter as a const reference.
The const qualifier is a promise that the function will not modify the vector, and passing the vector as a reference is to avoid copying the whole vector each time we call the function. This is a common practice. Now here's the main function for our demo code. This is where we'll declare our test vector. Notice how we need to specify the type of the elements in it. The syntax to do this is between angled brackets.
This will be a vector of integers and I'll name it numbers. Again, if we weren't using the std namespace, we'd have to specify it as std double colon vector. To some of us, that seems like a little too much trouble, and so I'll leave it at just vector. Here's an integer definition for the value that will be entered. Let's initialize it at zero.
Now let's report that we're pushing back at the end of vector, ask the user for a number, and collect it into the value variable. Let's make that a while loop. For this example, we'll only accept positive numbers, and whenever the user enters a negative number, the loop will end and go to the next round of insertions.
Please bear with me. The four rounds in this program will look very similar and almost identical in structure. So I'll show you what makes each of them unique. Once we have the value, we will insert it into the back of the vector with the push_back function. That's numbers.push_back, and we send in the value variable. And let's validate that it's not a negative number.
Now let's call the print function. And we are done with the first round of insertions, that is, pushing back. Now in order to compile and see what the code is doing, let's implement the print function. Here we have some code to print the beginning and the end of the message. So we'll display Vector content, open curly brackets, and at the end, we'll be closing the curly bracket and break a couple of lines.
Now between these two lines, we need to display the contents of the vector. This can be done the traditional C++ way, that is, in a loop with an iterator traversing the whole container, or we can do it the C++ 11 way, which is using a range for loop. This newer version of the for loop is much more concise. It resembles some versions of for loops seen in newer programming languages, such as Python.
This is the syntax we'll need to write a range for loop. Type in for, then within parentheses, just the variable that will get the element values. It has to be the same type as the element. Then colon, then the container. The iterating variable works very much like a function parameter. So avoiding making copies by declaring it as a reference is a common practice here too.
In the body of this loop, we just print out the iterating variable. Notice that since the iterating variable is not an iterator, it does not have to be the reference, and so we just print it out. We are ready to test our code, so let me hit compile and run. There's the pushing back message, so let's enter a round of numbers.
Let's say 1, 2, 3, and 4. Remember a negative number will end this program. Minus six. And that's the content of the vector, 1, 2, 3, 4.
- Why the STL is important
- Containers and iterators
- Vectors and lists
- Stacks and dequeues
- Associative containers