用于返回集合的所有子集的适当数据结构
我理解执行此操作的算法,但我不知道什么数据结构(数组、链表、向量、其他?)最适合返回最终的集合,因为我看到的每个示例都只要求打印集合。
- 有人可以解释一下在我提到的 3 种数据结构之间做出决定的思维过程吗?
- 另外,矢量还用了吗?我听说它们已经过时了,但仍然看到许多最近的例子。
需要明确的是,我的意思是所有子集,因此它们具有不同的大小。
I understand the algorithm for doing this but I don't know what data structure (array, linked list, vector, other?) would be best for returning the final set of sets since every example I see just asks to print the sets.
- Can someone explain the thought process for deciding between the 3 data structures I mentioned?
- Also, are vectors even used anymore? I heard they were obsolete but still see many recent examples.
To be clear, I mean ALL subsets, so they have different sizes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
决定使用哪种数据结构取决于:
普通数组将为您提供连续的内存块和对元素的随机访问,但是您需要知道预先确定元素的确切数量,以便您可以分配适当大小的数组。
使用 std::vector ,您可以随机访问数据,并且像数组一样是连续的,但向量是一个动态数组,它随着您添加新元素而增长,并且具有摊销常数复杂性,但是插入/删除元素的数量仅在末端更快,因为所有元素都需要移动。
使用 std::list 您无法获得随机访问,但元素的插入和删除速度更快,因为它只涉及指针链接的移动。
此外,矢量还被使用了吗?
这根本不是真的。
它们的使用非常广泛,并且是标准库提供的最广泛使用的数据结构之一。
The decision of which data structure to use depends on:
A normal array, would give you contiguous block of memory and random access to the elements, however you need to know the exact number of elements before hand so that you can allocate an array of appropriate size.
With
std::vector
you get random access to the data, and contiguous just like arrays but vector is a dynamic array,it grows as you add new elements, and a amortized constant complexity, however insertion/deletion of elements is faster only at the ends since all elements need to be moved.With
std::list
you don't get the random access but insertion and deletion of elements is faster because it involves just moving around of the pointer links.Also, are vectors even used anymore?
That is not true at all.
They are very much in use and one of the most widely used data structures provided by the Standard Library.
有一次我使用位字段来确定子集。其中,如果第
i
位为1
,则在子集中选择集合中的i
元素,并且0
> 否则。在这种情况下,您需要存储元素的顺序。我认为可以用 bool 向量来完成同样的事情。Once i used bit fields to determine the subsets. Where if the
i
th bit is1
then thei
the element in the set is selected in the subset and0
otherwise. In this case you need to store the ordering of the elements. The same can be done withbool
vectors i think.