解决C++中对象之间的依赖关系
我基本上有一个要创建的对象列表,但其中一些对象依赖于其他对象。 每个对象都包含它所依赖的对象的“ID”列表(在我的例子中为字符串)。
由此,我只需要一个有序列表,其中第一个元素将是没有依赖性的元素,最新的元素将是具有最大依赖性的元素。在此列表中逐一获取元素并创建它们应该可以顺利进行...
因此,从以下代码中,我想从对象中获取此列表:
typedef std::string Id;
typedef std::set < ID > Ids;
struct ObjectInformation
{
Id const& getId();
Ids const& getDependencies();
};
std::vector < ObjectInformation > objects;
我知道 BGL(boost 图形库可以做到这一点,但似乎有点太复杂了
I basically have a list of object to create, but some of them are dependant on other.
Each object holds a list of the "IDs" (string in my case) of the object it depends on.
From that I just need an ordered list, where the first element will be the one with no dependency and the latest one will be the one with the most dependency. Taking element one by one in this list and creating them should then work smoothly...
So from the following code, I'd like to get this list out of objects:
typedef std::string Id;
typedef std::set < ID > Ids;
struct ObjectInformation
{
Id const& getId();
Ids const& getDependencies();
};
std::vector < ObjectInformation > objects;
I know BGL (boost graph library can do it, but it seems a bit too complicated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是拓扑排序的一个主要示例。使用现有的拓扑排序实现是最简单的,所以我不确定为什么你取消 boost 的资格。请查看此文档这提供了一个例子。
This is a prime example for topological sort. It's easiest to use existing topological sort implementation, so I am not sure why you disqualify boost. Please have a look at this piece of documentation which provides an example.