• 问题反馈可发送邮件到stubbornhuang@qq.com

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 工资「喂饱肚子」,副业「养活灵魂」!

C++ – std::map正向遍历与反向遍历的几种方式

C++ 发布于2021-05-21 阅读 9,669次 0次评论 3次点赞 本文共1254个字,阅读需要4分钟。

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正向遍历与反向遍历的几种方式

原文链接:https://www.stubbornhuang.com/1357/

发布于:2021年05月21日 14:11:10

修改于:2023年06月26日 21:35:03

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
资源分享 - 3D Math Primer for Graphics and Game Development, First Edition 英文PDF下载
计算几何与计算机图形学资源
下一篇
资源分享 - 3D Math Primer for Graphics and Game Development, Second Edition 英文PDF下载
计算几何与计算机图形学资源
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号