Reputation: 567
using CPP map ,get warning info from eclipse editor.
Point find_shortest_node(const vector<Point> &openset,const map<Point, int> &f_score,const map<Point, vector<int> > &f_direction) {
vector<Point>::iterator iner_iterator = openset.begin();
Point min_point = *iner_iterator;
while (iner_iterator != openset.end()) {
if (f_score[*iner_iterator] < f_score[min_point]) {
min_point = *iner_iterator;
}
else if (f_score[*iner_iterator] == f_score[min_point]) {
vector<int> temp1 = f_direction[*iner_iterator], temp2 =f_direction[min_point];
if (temp1.size() < temp2.size()) {
min_point = *iner_iterator;
continue;
}
}
iner_iterator++;
}
return min_point;
}
warning info:
passing 'const std::map' as 'this' argument of '_Tp& std::map<_Key, _Tp,_Compare, _Alloc>::operator[](const _KT&) [with _KT = Point, _Key = Point, _Tp = int,_Compare = std::less, _Alloc = std::allocator >]'discards qualifiers [-fpermissive]
Upvotes: 0
Views: 185
Reputation: 726799
For some reason Eclipse thinks that f_score
is const
. According to your declaration, it is not const
, so this looks like a problem with Eclipse's editor.
If you have a C++11 - compliant compiler, you can work around this problem by using map::at
instead of the square brackets []
operator, like this:
while (iner_iterator != openset.end()) {
if (f_score.at(*iner_iterator) < f_score.at(min_point)) {
min_point = *iner_iterator;
} else if (f_score.at(*iner_iterator) == f_score.at(min_point)) {
vector<int> temp1 = f_direction.at(*iner_iterator), temp2 =f_direction.at(min_point);
if (temp1.size() < temp2.size()) {
min_point = *iner_iterator;
continue;
}
}
iner_iterator++;
}
Upvotes: 1
Reputation: 2017
The simplest solution is to replace
f_score[something]
with
f_score.find(something)->second
This works on const
references as it does not create a new element when something
is not found. You should check whether the element was actually found though:
map<Point, int>::const_iterator it = f_score.find(something);
if (it != f_score.end()) {
// ok, something was found
} else {
// element not found
}
Upvotes: 0