C++具有可交换元素的固定尺寸容器
我正在寻找具有以下功能的容器:
- 运行时固定大小。因此,内存不应该被分配成小块(就像 std::list 那样)。
- 元素应该是可交换的(例如
std::list::splice
提供)。
编辑: 考虑一个列表:我只需要将元素从任意位置移动到前面。 编辑2: 我想使用类似 std::list
的东西,但利用运行时固定大小。
I'm searching for a container with the following functionality:
- Fixed size at runtime. Thus, memory shouldn't be allocated in little chunks (like
std::list
does). - Elements should be swappable (Something like
std::list::splice
offers).
EDIT:
Thinking of a list: I just need to move elements from an arbitrary position to the front.
EDIT2:
I would like to use something like a std::list
, but takes advantage of a runtime fixed size.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会想到 TR1 数组:
或者,如果您还没有,
它在所有意图和目的上都是相同的。当然,元素上 std::swap 的有效性取决于正确的复制构造函数/赋值运算符的可用性。
I'd think of TR1 array:
Or, if you haven't got that yet,
Which is identical for all intents and purposes. Of course the validity of std::swap on elements depends on availability of proper copy constructor/assignment operator.
我不太清楚你在找什么。我想到了两种解决方案:
std::vector
(使用最大大小创建),加上对象的swap
的良好实现,或者的自定义分配器>std::list
,它预先分配单个块中所需的节点数量。It's not too clear to me what you are looking for. Two solutions come to mind:
std::vector
(created with the maximum size), coupled with a good implementation ofswap
for your objects, or a custom allocator forstd::list
, which pre-allocates the number of nodes you'll need in a single block.您可以在构造时指定
std::list
的(初始)大小:之后您仍然可以增大/缩小它,但列表从一开始就会包含 10 个默认构造的 Type 对象。
这符合您的需求吗?
You can specify the (initial) size of an
std::list
at construction time :You can still grow/shrink it afterwards, but the list will contain 10 default constructed Type objects from the start.
Does that suit your needs ?
std::向量?
基于数组。可以指定尺寸并具有交换功能。
您还没有说您将使用它做什么,所以我真的不知道您是否会遇到任何速度问题。
std::vector ?
Array based. Can specify the size and has a swap function.
You haven't said what you'll be using it for so I don't really know if you're going to have any speed issues with it.