编译代码时标准库中出现编译错误
Compile error in standard library when compiling code
我已经完成了一些 java 编码,但我对 c++ 完全陌生,不知道我的代码现在发生了什么。此代码在地图标准库中给我一个编译错误。它说:Cannot increment value of type ' std::_1::pari<int, int>'
,并且发生在 map
,insert(_InputIterator __f, _InputIterator __l)
如果有任何相关性的话。
我知道 Whosebug 社区通常不喜欢解决其他人的家庭作业,但我认为我真正尝试实现这一点,除此之外我对发生的事情非常好奇。
typedef std::pair<int, int> location;
std::set<location> neighbours(location loc, std::set<std::pair<location, location>> labyrinth, int& size) {
std::set<location> neighbours;
location locFmin = location(loc.first - 1, loc.second);
location locSmin = location(loc.first, loc.second - 1);
location locFplus = location(loc.first + 1, loc.second);
location locSplus = location(loc.first, loc.second + 1);
if (loc.first - 1 >= 0 && labyrinth.find(std::pair<location, location>(loc, locFmin)) == labyrinth.end()) {
neighbours.insert(locFmin);
}
if (loc.second - 1 >= 0 && labyrinth.find(std::pair<location, location>(loc, locSmin)) == labyrinth.end()) {
neighbours.insert(locSmin);
}
if (loc.first + 1 < size && labyrinth.find(std::pair<location, location>(loc, locFplus)) == labyrinth.end()) {
neighbours.insert(locFplus);
}
if (loc.second + 1 < size && labyrinth.find(std::pair<location, location>(loc, locSplus)) == labyrinth.end()) {
neighbours.insert(locSplus);
}
return neighbours;
}
int Labyrinth(std::set<std::pair<location, location>> labyrinth, int size) {
std::map<location, location> forest;
std::set<location> level;
std::set<location> known;
known.insert(location(0,0));
level.insert(location(0,0));
while (!level.empty()) {
std::set<location> nextLevel;
for (location loc: level) {
for (location neighbour: neighbours(loc, labyrinth, size)) {
if (known.find(neighbour) != known.end()) {
known.insert(neighbour);
forest.insert(neighbour, loc);
nextLevel.insert(neighbour);
}
}
}
level = nextLevel;
}
std::list<location> path;
location walk = location(size - 1, size - 1);
path.push_front(walk);
while (walk != location(0, 0)) {
walk = forest[walk];
path.push_front(walk);
}
int answ = path.size();
return answ;
}
这是一种算法,应该通过大小为 size
的方形迷宫执行广度优先搜索,当然还有大小 * 大小 location(x, y)
个对象。
传入列表 labyrinth
定义了迷宫中不能穿过的墙壁。最终函数应该 return 从 (0, 0) 到 (size - 1, size - 1) 的最短路径中包含的节点数。
这是对算法的简单测试
std::set<std::pair<location, location> > labyrinth;
labyrinth.insert(std::pair<location, location>(location(0, 0), location(1, 0)));
labyrinth.insert(std::pair<location, location>(location(0, 1), location(1, 1)));
labyrinth.insert(std::pair<location, location>(location(0, 2), location(0, 3)));
labyrinth.insert(std::pair<location, location>(location(1, 1), location(1, 2)));
labyrinth.insert(std::pair<location, location>(location(1, 2), location(2, 2)));
labyrinth.insert(std::pair<location, location>(location(2, 3), location(3, 3)));
labyrinth.insert(std::pair<location, location>(location(2, 2), location(3, 2)));
labyrinth.insert(std::pair<location, location>(location(2, 1), location(3, 1)));
int labAnswer = Labyrinth(labyrinth, 4);
std::cout << labAnswer << std::endl;
if (labAnswer == 13)
{
std::cout << "Correct" << std::endl;
}
else
{
std::cout << "Incorrect" << std::endl;
}
在任何人开始想出更好的想法来解决这个问题之前。我从一本关于算法的书的 bfs java 实现中得到了 bfs 代码的想法。我对更有效地解决这个难题不感兴趣,总会有更好的方法来做某事。我想知道我的代码发生了什么,可能还有我在这里缺少的 C++ 方面。
检查您的#include headers,您将需要
#include <map>
此外,如果您添加 using namespace std;
,那么您可以跳过向每个声明添加 std::
。
您没有正确使用 std::map 插入,
行
forest.insert(neighbour, loc);
应该是
forest[neighbor] = loc;
我已经完成了一些 java 编码,但我对 c++ 完全陌生,不知道我的代码现在发生了什么。此代码在地图标准库中给我一个编译错误。它说:Cannot increment value of type ' std::_1::pari<int, int>'
,并且发生在 map
,insert(_InputIterator __f, _InputIterator __l)
如果有任何相关性的话。
我知道 Whosebug 社区通常不喜欢解决其他人的家庭作业,但我认为我真正尝试实现这一点,除此之外我对发生的事情非常好奇。
typedef std::pair<int, int> location;
std::set<location> neighbours(location loc, std::set<std::pair<location, location>> labyrinth, int& size) {
std::set<location> neighbours;
location locFmin = location(loc.first - 1, loc.second);
location locSmin = location(loc.first, loc.second - 1);
location locFplus = location(loc.first + 1, loc.second);
location locSplus = location(loc.first, loc.second + 1);
if (loc.first - 1 >= 0 && labyrinth.find(std::pair<location, location>(loc, locFmin)) == labyrinth.end()) {
neighbours.insert(locFmin);
}
if (loc.second - 1 >= 0 && labyrinth.find(std::pair<location, location>(loc, locSmin)) == labyrinth.end()) {
neighbours.insert(locSmin);
}
if (loc.first + 1 < size && labyrinth.find(std::pair<location, location>(loc, locFplus)) == labyrinth.end()) {
neighbours.insert(locFplus);
}
if (loc.second + 1 < size && labyrinth.find(std::pair<location, location>(loc, locSplus)) == labyrinth.end()) {
neighbours.insert(locSplus);
}
return neighbours;
}
int Labyrinth(std::set<std::pair<location, location>> labyrinth, int size) {
std::map<location, location> forest;
std::set<location> level;
std::set<location> known;
known.insert(location(0,0));
level.insert(location(0,0));
while (!level.empty()) {
std::set<location> nextLevel;
for (location loc: level) {
for (location neighbour: neighbours(loc, labyrinth, size)) {
if (known.find(neighbour) != known.end()) {
known.insert(neighbour);
forest.insert(neighbour, loc);
nextLevel.insert(neighbour);
}
}
}
level = nextLevel;
}
std::list<location> path;
location walk = location(size - 1, size - 1);
path.push_front(walk);
while (walk != location(0, 0)) {
walk = forest[walk];
path.push_front(walk);
}
int answ = path.size();
return answ;
}
这是一种算法,应该通过大小为 size
的方形迷宫执行广度优先搜索,当然还有大小 * 大小 location(x, y)
个对象。
传入列表 labyrinth
定义了迷宫中不能穿过的墙壁。最终函数应该 return 从 (0, 0) 到 (size - 1, size - 1) 的最短路径中包含的节点数。
这是对算法的简单测试
std::set<std::pair<location, location> > labyrinth;
labyrinth.insert(std::pair<location, location>(location(0, 0), location(1, 0)));
labyrinth.insert(std::pair<location, location>(location(0, 1), location(1, 1)));
labyrinth.insert(std::pair<location, location>(location(0, 2), location(0, 3)));
labyrinth.insert(std::pair<location, location>(location(1, 1), location(1, 2)));
labyrinth.insert(std::pair<location, location>(location(1, 2), location(2, 2)));
labyrinth.insert(std::pair<location, location>(location(2, 3), location(3, 3)));
labyrinth.insert(std::pair<location, location>(location(2, 2), location(3, 2)));
labyrinth.insert(std::pair<location, location>(location(2, 1), location(3, 1)));
int labAnswer = Labyrinth(labyrinth, 4);
std::cout << labAnswer << std::endl;
if (labAnswer == 13)
{
std::cout << "Correct" << std::endl;
}
else
{
std::cout << "Incorrect" << std::endl;
}
在任何人开始想出更好的想法来解决这个问题之前。我从一本关于算法的书的 bfs java 实现中得到了 bfs 代码的想法。我对更有效地解决这个难题不感兴趣,总会有更好的方法来做某事。我想知道我的代码发生了什么,可能还有我在这里缺少的 C++ 方面。
检查您的#include headers,您将需要
#include <map>
此外,如果您添加 using namespace std;
,那么您可以跳过向每个声明添加 std::
。
您没有正确使用 std::map 插入,
行
forest.insert(neighbour, loc);
应该是
forest[neighbor] = loc;