是否可以设置stl :: deque的内部数组的大小?

时间:2017-04-05 03:05:57

标签: c++ stl

stl :: deque实现为数组数组; this questions 解释了在大多数情况下如何实施;我的问题是:是否可以设置内部数组(或块)的大小?它似乎是一个无法操纵的实现细节。是否有允许设置内部数组大小的实现?

感谢Pinky的回答:在libstdc ++(gcc stl)中我们有

#ifndef _GLIBCXX_DEQUE_BUF_SIZE
#define _GLIBCXX_DEQUE_BUF_SIZE 512
#endif

  inline size_t
  __deque_buf_size(size_t __size)
  { return (__size < _GLIBCXX_DEQUE_BUF_SIZE
            ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) : size_t(1)); }

因此,在包含deque标头(对于stdc ++)之前,通过定义_GLIBCXX_DEQUE_BUF_SIZE(#define _GLIBCXX_DEQUE_BUF_SIZE 4096 - 如果你想要一个这样大小的内部节点)来设置内部节点的大小

__ deque_buf_size甚至还有一些doxygen文档,因此记录了这个解决方案(即使它没有被标准所涵盖)。

  /**
   *  @brief This function controls the size of memory nodes.
   *  @param  __size  The size of an element.
   *  @return   The number (not byte size) of elements per node.
   *
   *  This function started off as a compiler kludge from SGI, but
   *  seems to be a useful wrapper around a repeated constant
   *  expression.  The @b 512 is tunable (and no other code needs to
   *  change), but no investigation has been done since inheriting the
   *  SGI code.  Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
   *  you are doing, however: changing it breaks the binary
   *  compatibility!!
  */

1 个答案:

答案 0 :(得分:-1)

你可以使用它的ctor:

std::deque<int> myDeque (size, defaultValue);

或调整大小功能:

std::deque<int> myDeque;
...
myDeque.resize(newSize);