From the course: C++ Standard Template Library

Deques - C++ Tutorial

From the course: C++ Standard Template Library

Start my 1-month free trial

Deques

- [Instructor] Now let's get to know the deque sequence container class. Recall that a deque is a sequence container that supports pushing and popping data from both ends, and also random access. So first let's look at line number five, where we include the deque header file. The way we'll do this demo is just like the case of the vector class. That is, with two rounds of insertion, one pushing on the back and the next one pushing on the front. Finally, we'll display the content of the whole container. So let's look at line number 10, and declare our new deque container. We'll use a deque of integers, and we will name it numbers. Next we need a temporary variable for the integers we are going to enter. Let's call it temp. And let's start with the firs round of inserts, so we'll notify that we are inserting at the back that is Pushing Back, and we will loop until we find a negative number. We ask for a number, assign it to the temporary variable, and if it is positive, we will insert it into the deque container. We'll do that by means of the push_back function. And we sent the temporary variable. And we are done with the first round of insertions. So now let's print out the content of the container so far, and this time I will inline all the code. I will not use a function. So I'll start at line 21, and I will use exactly the iterator we need. The one we need is the iterator of the class deque of int. And so I will write: deque<int>::iterator. We'll call our iterator lower case it; and we are ready to write the for loop. So I will open curly brackets, close curly brackets, and in line 23 is where the for loop will go. And this is how it goes: first we initialize the iterator that is numbers.begin, then we'll loop as long as we don't reach the end, increment the iterator, it++ will do. And the body of the loop consists only on displaying only on displaying each element. So we will send the element the iterator is having access to separated by spaces. So let's back up and see what will happen. In line 13 we notified the user that we will start the push back round. We need to break a line here, then we ask for each number, as long as they are positive, and all of these positive numbers will be pushed back. Finally we'll display the contents of the container. Let's see it working. So I will enter numbers from one to five. One, two, three, four, and five, now a negative number and the container has all of those numbers in order. Excellent. Now we are ready for the second round of insertions that would be push front. Since we need exactly the same code we have here but with a different function, I will have to copy and paste it so bear with me. I will copy this and paste it below. I will show you what needs to change. So let's head to line number 27. In this next stage, we will not push back but to the front, so I will change this to Pushing Front. We will still ask for numbers, and we will still push those numbers, but not to the back, but to the front. That is why in line 32 I am changing push_back to push_front. And that's all I need. Remember that we already have the code to display the whole container right here starting at line 35, which actually needs some modifications. Actually, just this line needs to go away because this is the declaration of the iterator. We don't need to declare it once more. Actually, we would get an error if we do. One more thing we need to do between the stages is to re-initialize the temp variable. We'll do that in line 27, and we will break one line in the terminal. We are ready to hit compile and run. This time I will start pushing back numbers with two digits that say 34, 43, 76, 38, and 22. Notice that these numbers are shown in the order they were entered. Now for the second round, I will enter numbers of one digit. So three, six, eight, four, and five now a negative number, and now notice how these numbers were entered at the other end. I hope you can see that the interface for the deque class is very similar to that of the vector class or of the list container class.

Contents