【发布时间】:2017-07-25 16:40:11
【问题描述】:
我已经编写了代码(参考下文)来计算从开始顶点到结束顶点的移动次数。
除了代码逻辑的验证之外,我对这段代码的编译错误有点不知所措。 我想我可以理解为什么会发生错误,但我不确定修复它们的最佳方法。所以我真的可以在那里寻求帮助。
下面列出了给我编译错误的行、我认为它们出现的原因以及到目前为止我收集到的内容。
1号
map<Vertex,int> distanceMap; // distance is the minimum distance required to reach this current visited vertex.
unordered_map<Vertex,Vertex> pathMap; // pathMap is to trace the path until current visited Vertex.
unordered_set<Vertex> Q;
我上面得到的错误是
no matching function for call to ‘std::unordered_map, std::pair
>::unordered_map()
no matching function for call to ‘std::unordered_set >::unordered_set()’
我已经声明了类型 Vertex 像这样
typedef pair<int,int> Vertex;
那么这里的错误是什么?
2号
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < cols; ++j)
{
Q.insert(make_pair(i,j));
}
}
当我将该对插入 unordered_set 时发生错误。
no matching function for call to ‘std::unordered_set >::insert(std::pair)’
3 号
for ( auto it = Q.begin(); it != Q.end(); ++it )
{
distanceMap[it++] = INT_MAX;
}
错误:
‘class std::unordered_set >’ has no member named ‘begin’
!‘class std::unordered_set >’ has no member named ‘end’
如果 auto 不适用,我该如何正确声明迭代器?
4号
Q.erase(Q.find(currVertex));
错误:
‘class std::unordered_set >’ has no member named ‘erase’
!‘class std::unordered_set >’ has no member named ‘find’
5号
distanceMap[*it] = updateDist;
pathMap[*it] = currVertex;
错误:
no match for ‘operator[]’ (operand types are ‘std::unordered_map, std::pair >’ and ‘std::pair’)
我知道这里需要某种运算符重载? 我将如何实现这一点?
6号
while(pmap[curr])
// COMPILE ERROR : no match for ‘operator[]’ (operand types are ‘std::unordered_map, std::pair >’ and ‘Vertex {aka std::pair}’)
{
moves+=1;
curr = pmap[curr];
// COMPILE ERROR : no match for ‘operator[]’ (operand types are ‘std::unordered_map, std::pair >’ and ‘Vertex {aka std::pair}’)
}
同样,我不确定为什么当我已经声明 pmap 的类型为 unordered_map<Vertex,Vertex> pmap 时会出现错误
和typedef pair<int,int> Vertex;
完整代码供参考
// ------ UTIL -------
/* all required structures for Graph */
typedef pair<int,int> Vertex;
/* all criteria required to get a vertex */
typedef pair<Vertex, int> MyPairType;
struct CompareSecond
{
bool operator()(const MyPairType& left, const MyPairType& right) const
{
return left.second < right.second;
}
};
Vertex get_min_distance_vertex(map<Vertex,int> distMap)
{
pair<Vertex, int> min = *min_element(distMap.begin(), distMap.end(), CompareSecond());
return min.first; // compare (int) distances to get min dist, return vertex.
}
/* count moves */
int countMoves(Vertex target, unordered_map<Vertex,Vertex> pmap)
{
int moves = 0;
Vertex curr = target;
while(pmap[curr])
// COMPILE ERROR : no match for ‘operator[]’ (operand types are ‘std::unordered_map, std::pair >’ and ‘Vertex {aka std::pair}’)
{
moves+=1;
curr = pmap[curr];
// COMPILE ERROR : no match for ‘operator[]’ (operand types are ‘std::unordered_map, std::pair >’ and ‘Vertex {aka std::pair}’)
}
return moves;
}
/* get neighbours */
vector<Vertex> get_neighbours(Vertex curr, int rows, int cols)
{
vector<Vertex> n;
if (curr.first <rows && curr.second <cols)
{
if( curr.first+1 < cols && curr.second+2 < rows)
n.push_back(make_pair(curr.first+1,curr.second+2));
if( curr.first+2 < cols && curr.second+1 < rows)
n.push_back(make_pair(curr.first+2,curr.second+1));
if( curr.first+2 < cols && curr.second-1 < rows)
n.push_back(make_pair(curr.first+2,curr.second-1));
if( curr.first+1 < cols && curr.second-2 < rows)
n.push_back(make_pair(curr.first+1,curr.second-2));
if( curr.first-1 < cols && curr.second+2 < rows)
n.push_back(make_pair(curr.first-1,curr.second+2));
if( curr.first-2 < cols && curr.second+1 < rows)
n.push_back(make_pair(curr.first-2,curr.second+1));
if( curr.first-2 < cols && curr.second-1 < rows)
n.push_back(make_pair(curr.first-2,curr.second-1));
if( curr.first-1 < cols && curr.second-2 < rows)
n.push_back(make_pair(curr.first-1,curr.second-2));
}
return n;
}
/* get distance */
int distance_between_vertices(Vertex source, Vertex target)
{
float x = (source.first - target.first)*(source.first - target.first);
float y = (source.second - target.second)*(source.second - target.second);
return (int) sqrt(x+y);
}
// ---------------
int minimumMoves(int rows, int cols, int startx, int starty, int endx, int endy) {
Vertex source = make_pair(startx,starty);
Vertex target = make_pair(endx,endy);
map<Vertex,int> distanceMap; // distance is the minimum distance required to reach this current visited vertex.
unordered_map<Vertex,Vertex> pathMap; // pathMap is to trace the path until current visited Vertex.
unordered_set<Vertex> Q; // Q is a set of vertices in chessboard graph.
// initialize Graph ===> O(N^2) assuming rows = cols = n in a chessboard.
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < cols; ++j)
{
Q.insert(make_pair(i,j));
}
}
// Iterate over set, and then assign distanceMap and pathMap values.
auto it = Q.begin();
for ( auto it = Q.begin(); it != Q.end(); ++it )
{
distanceMap[it++] = INT_MAX;
}
// Make distanceMap[source] = 0;
distanceMap[source] = 0;
// Dijkstra's.
// Ref 1 : finding minimum in map -- http://stackoverflow.com/questions/2659248/finding-minimum-value-in-a-map
while(!Q.empty())
{
Vertex currVertex = get_min_distance_vertex(distanceMap);
if(currVertex.first==target.first && currVertex.second==target.second){
return countMoves(currVertex, pathMap);
}
Q.erase(Q.find(currVertex));
vector<Vertex>neighbours = get_neighbours(currVertex,rows,cols);
if(neighbours.empty())
break;
for(auto it = neighbours.begin(); it!= neighbours.end();it++)
{
int updateDist = distanceMap[currVertex] + distance_between_vertices(currVertex,*it);
if (updateDist < distanceMap[*it])
{
distanceMap[*it] = updateDist;
pathMap[*it] = currVertex;
}
}
}
// return all valid distances here.
return -1; //no possible move.
}
【问题讨论】:
-
您应该在出现编译器错误时解决它们,而不是让它们累积直到您不知所措。
-
是的,当我之前编写过自定义数据类型并且没有出现这些错误并且考虑到没有参考时,我只是不确定如何修复它们。如果您能指出一些理解这些错误的参考资料,将会很有帮助:)
-
我投票结束这个,因为它问了太多问题。您需要一一解决错误,因为偶尔解决一个问题可以解决其余问题。在这种情况下,您似乎对
std::unordered_map有很多问题。你真的应该把你的代码减少到minimal reproducible example。你是用 C++11 编译的吗?,因为std::unordered_mapwasn't introduced till C++11 -
我明白这一点,但这正是我的问题......如何在语法上解决编译错误?我有这些 unordered_map 和 set 错误,我正在用 C++11 编译器编译它们。但这些错误仍然存在。代码审查会是提出这个问题的更好地方吗?
-
你在这行中使用什么作为键?
std::unordered_map<Vertex> Q;unordered_maps 需要一个密钥。
标签: c++ graph compiler-errors dijkstra