如何从地图的末尾开始

How to start at end of map

本文关键字:开始 地图      更新时间:2023-10-16

我希望能够从std::map的末尾开始,以便首先检查列表末尾的项目,例如。。。

std::map<int> MapBasicExample;
MapBasicExample.insert(1);
MapBasicExample.insert(2);
MapBasicExample.insert(3);
MapBasicExample.insert(4);
MapBasicExample.insert(5);

当我使用MapBasicExample.begin()时,首先检查的是值"5",并且我希望首先从循环中的1-5开始,而不是像使用MapBasicExample.begin()时那样从5-1开始,如何做到这一点?

您可以按以下相反顺序迭代映射:

for (auto it = MapBasicExample.rbegin(); it != MapBasicExample.rend(); it++) {
    std::cout << it->first << " = " << it->second << std::endl;
}

请记住,贴图有关键点和值。你的例子看起来像一套。不过,以相反的顺序对它们进行迭代的逻辑是相似的。