有向邻接表图中不同大小边的存储
Storage of different-sized edges in directed adjacency-list graph
本文关键字:存储 更新时间:2023-10-16
设置如下:
class Vertex;
class Edge {
Vertex *to;
};
class Teleport : public Edge {
int teleportCost;
};
class Walk : public Edge {
double distance;
};
class Vertex {
vector<Edge> adjacencies; // can only store Edge and not Walk, Teleport etc!
};
问题当然是Vertex
可以有许多不同类型的邻接,每个邻接都有不同的字节大小。那么,存储这些边的最好方法是什么呢?我的图永远不会移除顶点或边;一旦它被完全构建,它就会保存在内存中,直到它被一次全部销毁。也许这将允许在一个大内存池或足够大的vector
中"全局"存储字节,然后每个Vertex
都有一个vector<void*>
或vector<Edge*>
或指向内存缓冲区的其他东西。然而,我不确定如何做到这一点。有没有人可以提供一些指导,告诉我在我的场景中最好使用什么作为存储?
所有的Edge
将被多态使用,所以我从不需要转换到Teleport
等。
当一个类型被指定为子类型时,这种类型的vector或对象数组几乎总是错误的。您需要存储一个向量或指针数组。
代替:
vector<Edge> adjacencies;
使用
vector<std::share_ptr<Edge>> adjacencies;
相关文章:
- 将字符串存储在c++中的稳定内存中
- std::原子加载和存储都需要吗
- C++:将控制台输出存储在宏中更好吗
- 使用QProcess执行命令,并将结果存储在QStringList中
- 访问存储在向量C++中的结构的多态成员
- 如何从存储在std::映射中的std::集中删除元素
- 存储模板类型以强制转换回派生<T>
- 类型总是使用其大小存储在内存中吗
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 使用无符号字符数组有效存储内存
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 带结构的二维矢量:如何存储元素
- 添加存储在向量中的大整数的函数出现问题
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 在std::vector上存储带有模板的类实例
- 谷歌测试中的期望值存储在哪里
- 为什么C中的通用链表中存储的数据已损坏
- 在c++中获取两个大int,并将它们存储在数组中
- 在reactor中存储eventHandlers的最佳方式是什么