问题
I am using a std::deque for storing a large collection of items.
I know that deques is implemented as a list of vectors. The size of those vectors cannot be set but I wander what is the algorithm for choosing that size.
回答1:
deque is implemented as a vector of vectors (a list of vectors would impede the constant time random access). The size of the secondary vectors is implementation dependent, a common algorithm is to use a constant size in bytes.
回答2:
My deque
implementation, the one from GNU which is derived from the HP/SGI version, is not a list of vectors; at least, not an std::list
of std::vector
s. The comments state
* In previous HP/SGI versions of deque, there was an extra template
* parameter so users could control the node size. This extension turned
* out to violate the C++ standard (it can be detected using template
* template parameters), and it was removed.
*
* Here's how a deque<Tp> manages memory. Each deque has 4 members:
*
* - Tp** _M_map
* - size_t _M_map_size
* - iterator _M_start, _M_finish
*
* map_size is at least 8. %map is an array of map_size
* pointers-to-"nodes". (The name %map has nothing to do with the
* std::map class, and "nodes" should not be confused with
* std::list's usage of "node".)
来源:https://stackoverflow.com/questions/5728359/stl-internals-deque-implementation