Item 7. Using vector and deque

I l @ ve RuBoard

Item 7. Using vector and deque

Difficulty: 3

What is the difference between vector and deque? When should you use each one? And how can you properly shrink such containers when you no longer need their full capacity? These answers and more, as we consider news updates from the standards front.

  1. In the standard library, vector and deque provide similar services. Which should you typically use? Why? Under what circumstances would you use the other?

  2. What does the following code do?

     vector<C> c( 10000 ); c.erase( c.begin()+10, c.end() ); c.reserve( 10 ); 
  3. A vector or deque typically reserves extra internal storage as a hedge against future growth, to prevent too-frequent reallocation as new elements are added. Is it possible to completely clear a vector or deque (that is, not only remove all contained elements, but also free all internally reserved storage)? Demonstrate why or why not.

    Warning: Answers 2 and 3 may be subtle. Each has a facile answer, but don't stop at the surface; try to be as detailed as possible.

I l @ ve RuBoard


More Exceptional C++
More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions
ISBN: 020170434X
EAN: 2147483647
Year: 2001
Pages: 118
Authors: Herb Sutter

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net