【发布时间】:2013-12-16 22:35:58
【问题描述】:
我在一个类中编写了这样的代码来计算从源到节点的最大距离。现在我需要把它从课堂上拿出来,并有一个类似的函数来计算我的距离,但要使用 Djikstra。因此,我需要将这个城市向量和源作为我的顶点之一传递(这将遍历顶点)并将该函数的距离返回给我的下一次计算。我的时间不多了,请帮帮我。
int distanceToNearCity(int cityIdOfStore, const std::vector<City> & AllCities) const
{
// is there a store in this city ?
if (storeExists || cityId == cityIdOfProposedNewStore)
{
return 0; // 0 distance
}
int distance = TOOFAR; // initialise with more than max allowed
for (int i=0; i<connectingCities.size(); ++i)
{
int id = connectingCities[i];
if (AllCities[id-1].hasStore() || AllCities[id-1].getId() == cityIdOfProposedNewStore)
{
// we have a store (or proposed one) here, but is it's distance more than other stores ?
if (distances[i] < distance)
{
distance = distances[i];
}
}
}
return distance;
}
如何将这些类对象传递给公开的函数。谢谢!!
【问题讨论】:
-
当你说你需要把它带出课堂时,你的意思是你需要把它变成一个静态/全局函数吗?我不清楚你的问题到底是什么。
-
是的。因为我需要图表的每个顶点值才能从 main 访问它。我想让它成为全球性的。我正在尝试使用 Dijkstra 解决所有对的距离。所以,当我在循环时有一个新的源时,我可以在函数调用中使用它。
-
如何让
distanceToNearCity虚拟化,从当前类派生并重新定义距离算法? -
您希望 1) 使其成为非成员函数,以及 2) 将算法更改为 Dijkstra。您需要哪方面的帮助?
-
@Beta 我在这两个方面都需要帮助。更多地关注(1)然后我需要知道如何将参数传递给这个非成员函数。
标签: c++ algorithm graph graph-algorithm