As you can see the vector is very powerful and has a whole bunch of useful

# As you can see the vector is very powerful and has a

This preview shows page 55 - 57 out of 548 pages.

(see below), all iterators are invalidated. As you can see, the vector is very powerful and has a whole bunch of useful functions. Others exist as well, so be sure to consult a reference. deque : Another sequence container The STL vector is a linear sequence that grows and shrinks dynamically. While it's possible to add and remove elements anywhere in a vector , insertions and deletions are fastest at the end of the vector and are costly at the beginning. If we want to work with a sequence of elements that grows and shrinks at both ends, we could do so using a vector , but would suffer a severe performance hit. Is there some way that we can model a linear se- quence with efficient insertion and deletion at both ends? As mentioned at the start of this chapter, each of the CS106B/X ADTs (excluding Grid ) has a corresponding STL container. However, the STL has several containers which have no analog in the CS106B/X ADTs. One such container is the deque . deque (pronounced “deck,” as in a deck of cards, and not “dequeue,” the queue's
- 50 - Chapter 4: STL Containers, Part I remove element operation) stands for “ d ouble- e nded que ue” and is a sequence container like vector that sup- ports efficient insertion and removal of elements at both the beginning and the end of its sequence. What's inter- esting about the deque is that all operations supported by vector are also provided by deque . Thus we can resize a deque , use the bracket syntax to access individual elements, and erase elements at arbitrary positions. However, deque s also support two more functions, push_front and pop_front , which work like the vec- tor 's push_back and pop_back except that they insert and remove elements at the front of the deque . These four functions ( push_back , pop_back , push_front , pop_front ) allow the deque to mimic other data struc- tures we've discussed this chapter. For example, a deque can act like a stack if we use push_front and pop_front to insert and remove elements at the beginning of its sequence, and similarly could model a queue by calling pop_front to remove elements and push_back to add them. This is not a coincidence – the stack is actually implemented as a wrapper class that manipulates a deque , * as is the queue . Because the stack and queue “adapt” the deque from one data type to another, they are not technically containers, but “container ad- apters.” If deque has more functionality than vector , why use vector ? The main reason is speed. deque s and vec- tor s are implemented in two different ways. Typically, a vector stores its elements in contiguous memory ad- dresses. deque s, on the other hand, maintain a list of different “pages” that store information. This is shown here: These different implementations impact the efficiency of the vector and deque operations. In a vector , be- cause all elements are stored in consecutive locations, it is possible to locate elements through simple arithmetic: to look up the nth element of a vector , find the address of the first element in the vector , then jump forward n positions. In a deque

#### You've reached the end of your free preview.

Want to read all 548 pages?