Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我怎样才能在列表中找到国家的邻国? 我正在用C++编程游戏风险。在这个游戏中,你可以向与你的国家接壤的国家宣战_C++_Algorithm_List - Fatal编程技术网

我怎样才能在列表中找到国家的邻国? 我正在用C++编程游戏风险。在这个游戏中,你可以向与你的国家接壤的国家宣战

我怎样才能在列表中找到国家的邻国? 我正在用C++编程游戏风险。在这个游戏中,你可以向与你的国家接壤的国家宣战,c++,algorithm,list,C++,Algorithm,List,但我能找出检验两国是否是“邻居”的唯一方法是一个巨大的if检验 以下是名单声明: //Europe: gebieden << CGebied("iceland","europe"); gebieden << CGebied("scandinavia","europe"); gebieden << CGebied("great-britain", "europe"); gebieden << CGebied("northern-europe","eu

但我能找出检验两国是否是“邻居”的唯一方法是一个巨大的if检验

以下是名单声明:

//Europe:
gebieden << CGebied("iceland","europe");
gebieden << CGebied("scandinavia","europe");
gebieden << CGebied("great-britain", "europe");
gebieden << CGebied("northern-europe","europe");
gebieden << CGebied("western-europe","europe");
gebieden << CGebied("southern-europe","europe");
gebieden << CGebied("ukraine", "europe");
//Asia
gebieden << CGebied("ural", "asia");
gebieden << CGebied("siberia", "asia");
gebieden << CGebied("yakutsk", "asia");
gebieden << CGebied("kamchatka", "asia");
gebieden << CGebied("irkutsk", "asia");
gebieden << CGebied("mongolia", "asia");
gebieden << CGebied("afghanistan", "asia");
gebieden << CGebied("china", "asia");
gebieden << CGebied("japan", "asia");
gebieden << CGebied("middle-east", "asia");
gebieden << CGebied("india", "asia");
gebieden << CGebied("siam", "asia");
//Australië
gebieden << CGebied("indonesia", "australia");
gebieden << CGebied("new-guinea", "australia");
gebieden << CGebied("western-australia", "australia");
gebieden << CGebied("eastern-australia", "australia");
//Africa
gebieden << CGebied("egypt", "africa");
gebieden << CGebied("north-africa", "africa");
gebieden << CGebied("east-africa", "africa");
gebieden << CGebied("congo", "africa");
gebieden << CGebied("south-africa", "africa");
gebieden << CGebied("madagascar", "africa");
//North-America
gebieden << CGebied("alaska","n-america");
gebieden << CGebied("northwest territory", "n-america");
gebieden << CGebied("alberta", "n-america");
gebieden << CGebied("ontario", "n-america");
gebieden << CGebied("quebec", "n-america");
gebieden << CGebied("greenland", "n-america");
gebieden << CGebied("western-us", "n-america");
gebieden << CGebied("eastern-us", "n-america");
gebieden << CGebied("central-america", "n-america");
//South-America
gebieden << CGebied("venezuela", "s-america");
gebieden << CGebied("brazil", "s-america");
gebieden << CGebied("peru", "s-america");
gebieden << CGebied("argentina", "s-america");
有没有更有效的方法?
谢谢

您使用什么样的数据结构来存储您的法院

我建议采用图表式结构:为每个国家保留一份邻居列表(
std::set
)。通过这种方式,你可以很容易地检查另一个国家是否在它的邻国名单中

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct country{
    vector<country*> neighbours;
    //various other properties of the country.
    string name;
};

int main()
{
    country country1;
    country country2;
    country1.name = "The first country";
    country2.name = "The second country";
    //Make country1 and country 2 neighbours.
    country1.neighbours.push_back(&country2);
    country2.neighbours.push_back(&country1);

    cout<<country1.name<<" is a neighbour with "<<country1.neighbours[0]->name<<endl;

    return 0;
}
另一种可能性是:

bool isNeighbour[COUNTRY_COUNT][COUNTRY_COUNT];
这比
c++
更重要。它假定您所在的县已编号,并允许您像这样查找相邻关系


但请记住,您必须两次填写每个关系,以使它们相互关联。

您可以在每个国家结构中使用指针向量,创建一个类似于“流程图”的引用,以引用所有表示国家邻居的国家

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct country{
    vector<country*> neighbours;
    //various other properties of the country.
    string name;
};

int main()
{
    country country1;
    country country2;
    country1.name = "The first country";
    country2.name = "The second country";
    //Make country1 and country 2 neighbours.
    country1.neighbours.push_back(&country2);
    country2.neighbours.push_back(&country1);

    cout<<country1.name<<" is a neighbour with "<<country1.neighbours[0]->name<<endl;

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
结构国家{
矢量邻居;
//该国的各种其他财产。
字符串名;
};
int main()
{
国家1;
国家2;
country1.name=“第一个国家”;
country2.name=“第二个国家”;
//让country1和Country2成为邻居。
country1.邻居。推回(&country2);
country2.邻居。推回(&country1);

我认为使用一个巨大的“如果”不是正确的方法

我会尝试创建一个
类地图
,在该类中,我会使用每个国家的名称作为一个函数,它已经拥有我需要的信息,在这种情况下,它是邻居

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct country{
    vector<country*> neighbours;
    //various other properties of the country.
    string name;
};

int main()
{
    country country1;
    country country2;
    country1.name = "The first country";
    country2.name = "The second country";
    //Make country1 and country 2 neighbours.
    country1.neighbours.push_back(&country2);
    country2.neighbours.push_back(&country1);

    cout<<country1.name<<" is a neighbour with "<<country1.neighbours[0]->name<<endl;

    return 0;
}

这样做,每当我想发现哪些国家是邻国时,我只需要调用带有该国功能的
类映射,如
country.ital(邻国)
或只是
country.itali()
,其中国家为对象,意大利为类映射中函数的名称。

创建邻居的
映射。您的问题是:"嘿,我有一个我正在存储的国家列表,大概是在某种结构中,我不会解释,我现有的边境国家测试非常低效,在某种程度上我也不会解释,但我希望有人告诉我如何提高效率,而不让我显示实际低效的代码,只是模糊地描述这是一个“巨大的如果测试”;因此每个人都必须使用他们神奇的心灵射线束机器来查看我头脑中的代码,并告诉我如何使它变得更好".这是对你问题的准确描述吗?是的,这很准确,我会上传我的代码:)列表中有乌克兰,但没有俄罗斯。俄罗斯被分成了几个部分。你有没有可能是一个有洞察力的人?我用的是我在互联网上找到的地图;)嗯,字符串比较相对来说需要很多工作。你可以尝试查找你的国家按id。如果您将它们存储在
QSet
中,查找一个国家只需
O(log(n))
时间。(我不知道
gebieden
的类型,但您可以执行
gebieden[“阿拉斯加”]。
如果您实现
CGebied::isnexter