如何使用C+&#x2B中的键从映射中获得配对。
我有以下映射:
std::map<char, std::pair<int, int> > robots;
我正在使用此功能来填充输入符合某些条件的映射:
bool World::addRobot(int row, int col, char robot_name) {
// This if block checks if the desired location is a valid 1 and keeps a track of all the robots already in the grid
if (map_[row][col] == '1' && robots.find(robot_name) == robots.end()){
map_[row][col] = robot_name;
robots.insert(make_pair(robot_name, std::make_pair(row, col)));
}
else{std::cout << "Invalid input" << std::endl;}
return true;
}
每个机器人名称(仅是一个char)都用其位置保存,而其位置仅是行/col坐标。在以下功能中,我希望能够检索&amp; ETHE位置对给定机器人名称:
std::pair<int, int> World::getRobot(char robot_name) {
std::pair<int, int> location = robots.find(robot_name);
return location;
}
但是名称位置
是带有以下错误消息的红线:
No viable conversion from 'std::map<char, std::pair<int, int>>::iterator' (aka '_Rb_tree_iterator<std::pair<const char, std::pair<int, int>>>') to 'std::pair<int, int>'
我要在哪里出错?如何仅从机器人名称中返回坐标对?
I have the following map:
std::map<char, std::pair<int, int> > robots;
I am using this function to populate the map given the input meets certain conditions:
bool World::addRobot(int row, int col, char robot_name) {
// This if block checks if the desired location is a valid 1 and keeps a track of all the robots already in the grid
if (map_[row][col] == '1' && robots.find(robot_name) == robots.end()){
map_[row][col] = robot_name;
robots.insert(make_pair(robot_name, std::make_pair(row, col)));
}
else{std::cout << "Invalid input" << std::endl;}
return true;
}
Each robot name, which is just a single char, is saved with a pair of its location, which are just row/col coordinates. In the following function, I want to be able to retrieve & ethe location pairs given the robot name:
std::pair<int, int> World::getRobot(char robot_name) {
std::pair<int, int> location = robots.find(robot_name);
return location;
}
But the name location
is redlines with the following error message:
No viable conversion from 'std::map<char, std::pair<int, int>>::iterator' (aka '_Rb_tree_iterator<std::pair<const char, std::pair<int, int>>>') to 'std::pair<int, int>'
Where am I going wrong? How can I return the coordinate pairs from just the robot name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
映射“指向”
std :: Pair&lt; const键,value&gt;
的迭代器。对于您的地图,
键
是char
,value
isstd :: pair&lt&lt; int,int&gt;
o在您的代码中,而不是:
您需要:
另外,您需要检查并查看查找的调用是否无法找到所需的密钥。在这种情况下,迭代器将等于
robots.end
,您必须处理:An iterator for a map "points to" a
std::pair<const KEY, VALUE>
.For your map, the
KEY
ischar
and theVALUE
isstd::pair<int, int>
So in your code, instead of:
you need to:
Also, you need to check and see if the call to find fails to find the key you want. In that case the iterator will be equal to
robots.end
, and you'll have to deal with that:std :: map :: Map :: find
将“逻辑指针”的地图迭代器返回到std ::
第二个
该对的成员是 value 您寻求从getrobot
(本身就是一对int
) /代码> S)。固定版本:
2附加说明:
auto
,它在使用迭代器时非常方便(而不是指定长迭代器类型)。std::map::find
returns a map iterator which is a "logical pointer" to astd::pair
of key and value of the element in the map (not only the value).The
second
member of this pair is the value that you seek to return fromgetRobot
(which is by itself a pair ofint
s).Fixed version:
2 additional notes:
auto
which is very convenient when using iterators (instead of specifying the long iterator type).