1 std::map正向遍历

1.1 for循环

#include <iostream>
#include <string>
#include <map>

int main()
{
    std::map<int, std::string> t_Map;
    t_Map[0] = "A";
    t_Map[1] = "B";
    t_Map[2] = "C";

    std::map<int, std::string>::iterator iter1;
    for (iter1 = t_Map.begin();iter1 != t_Map.end();iter1++)
    {
        std::cout << iter1->first << " : " << iter1->second << std::endl;
    }

    getchar();
    return 0;
}

1.2 while循环

#include <iostream>
#include <string>
#include <map>

int main()
{
    std::map<int, std::string> t_Map;
    t_Map[0] = "A";
    t_Map[1] = "B";
    t_Map[2] = "C";

    std::map<int, std::string>::iterator iter2 = t_Map.begin();
    while (iter2 != t_Map.end())
    {
        std::cout << iter2->first << " : " << iter2->second << std::endl;
        iter2++;
    }


    getchar();
    return 0;
}

2 std::map反向遍历

2.1 for循环

#include <iostream>
#include <string>
#include <map>

int main()
{
    std::map<int, std::string> t_Map;
    t_Map[0] = "A";
    t_Map[1] = "B";
    t_Map[2] = "C";

    std::map<int, std::string>::reverse_iterator iter1;
    for (iter1 = t_Map.rbegin();iter1 != t_Map.rend();iter1++)
    {
        std::cout << iter1->first << " : " << iter1->second << std::endl;
    }

    getchar();
    return 0;
}

2.2 while循环

#include <iostream>
#include <string>
#include <map>

int main()
{
    std::map<int, std::string> t_Map;
    t_Map[0] = "A";
    t_Map[1] = "B";
    t_Map[2] = "C";

    std::map<int, std::string>::reverse_iterator iter2 = t_Map.rbegin();
    while (iter2 != t_Map.rend())
    {
        std::cout << iter2->first << " : " << iter2->second << std::endl;
        iter2++;
    }


    getchar();
    return 0;
}

如果您觉得对您有帮助,可以请站长喝一杯咖啡哦!

记得在赞赏备注里写上您的昵称

您可在本站资助名单中查看你的打赏记录哦!

支付宝扫一扫

微信扫一扫

金额随意,礼清义重

当前分类随机文章推荐

全站随机文章推荐

关于本站站长 StubbornHuang
C++ – std::map正向遍历与反向遍历的几种方式-StubbornHuang Blog纵使晴明无雨色,入云深处亦沾衣。