如何使用C+&#x2B中的键从映射中获得配对。

发布于 2025-02-04 14:49:11 字数 1173 浏览 3 评论 0原文

我有以下映射:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难忘№最初的完美 2025-02-11 14:49:11

映射“指向” std :: Pair&lt; const键,value&gt;的迭代器。

对于您的地图,charvalue is std :: pair&lt&lt; int,int&gt;

o在您的代码中,而不是:

std::pair<int, int> location = robots.find(robot_name);

您需要:

std::pair<int, int> location = robots.find(robot_name)->second;

另外,您需要检查并查看查找的调用是否无法找到所需的密钥。在这种情况下,迭代器将等于robots.end,您必须处理:

const auto it = robots.find(robot_name);
if (it != robots.end()) {
    return it->second;
} else {
    // Not found, do something else
}

An iterator for a map "points to" a std::pair<const KEY, VALUE>.

For your map, the KEY is char and the VALUE is std::pair<int, int>

So in your code, instead of:

std::pair<int, int> location = robots.find(robot_name);

you need to:

std::pair<int, int> location = robots.find(robot_name)->second;

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:

const auto it = robots.find(robot_name);
if (it != robots.end()) {
    return it->second;
} else {
    // Not found, do something else
}
夏九 2025-02-11 14:49:11

std :: map :: Map :: find 将“逻辑指针”的地图迭代器返回到 std ::
第二个该对的成员是 value 您寻求从getrobot (本身就是一对int) /代码> S)。

固定版本:

std::pair<int, int> World::getRobot(char robot_name) 
{
    auto it = robots.find(robot_name);
    if (it == robots.end())
    {
        return std::pair<int, int>(0, 0);   // return some default value
    }
    return it->second;
}

2附加说明:

  1. 我使用auto,它在使用迭代器时非常方便(而不是指定长迭代器类型)。
  2. 我添加了一个检查密钥是否在地图中。如果不是 - 返回一些默认值。您可以更改默认值,或更改方法原型以返回在这种情况下的错误状态。

std::map::find returns a map iterator which is a "logical pointer" to a std::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 from getRobot (which is by itself a pair of ints).

Fixed version:

std::pair<int, int> World::getRobot(char robot_name) 
{
    auto it = robots.find(robot_name);
    if (it == robots.end())
    {
        return std::pair<int, int>(0, 0);   // return some default value
    }
    return it->second;
}

2 additional notes:

  1. I used auto which is very convenient when using iterators (instead of specifying the long iterator type).
  2. I added a check whether the key is in the map. If not - return some default value. You can change the default value, or change the method prototype to return an error status in this case.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文