C,稀疏二维矩阵的数据结构问题
我想知道如果我知道元素将是短类型,我可以将什么数据结构用于稀疏二维矩阵。我打算使用链表,但元素类型(短)有什么区别吗?如果元素将是整数类型而不是短类型,那么它将如何改变数据结构呢?
I was wondering what data structure I could use for a sparse 2d matrix if I know the element will be a short type. I was going to use a linked list but the element type (short) make any difference? What if the elements are going to be a integer type instead of short type then how would it change the data structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不过,该值的向量
可能最终会使用 4 个字节(结构成员通常会填充到 32 位边界)如果您确实缺乏空间并且 x,y 坐标有限,您可以将整个内容打包到 64 位的位中int
如果要添加和删除点,则可能需要使用列表,但通常稀疏数组具有一组固定的点或查找占主导地位,并且值得在添加点后按 X,Y 对它们进行排序。
如果您确实需要添加大量点并且仍然希望通过 x,y 快速访问,那么树可能值得一看
Vector of
The value is probably going to end up using 4bytes though (struct members are typically padded to 32bit boundaries) If you are really short of space and the x,y coords are limited you could pack the whole thing into the bits of a 64bit int
If you are adding and removing points then there might be a case for a list, but typically sparse arrays have a fixed set of points or lookup dominates and it's worth resorting them by X,Y after adding points.
If you do need to add lots of points AND still want fast access by x,y then a tree might be worth looking at
在纯C中,可以使用指针来存储不同类型的数据,并注意避免内存泄漏。
像这样:
考虑使用 C++ 模板怎么样?
In pure C, you can use pointer to store data of different types, and pay attention to avoid memory leaking.
like this:
How about considering using C++ template?