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

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

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

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

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

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

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

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

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

C++STL容器 – std::map删除指定元素

C++ 发布于2022-02-25 阅读 4,303次 0次评论 0次点赞 本文共2282个字,阅读需要6分钟。

1 for循环遍历std::map删除指定元素

1.1 第一种方式

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

void PrintMap(const std::map<int, std::string>& in_map)
{
    std::map<int, std::string>::const_iterator iter;
    for (iter = in_map.begin(); iter != in_map.end(); iter++)
    {
        std::cout << iter->first << " " << iter->second << std::endl;
    }
}

int main()
{
    std::map<int, std::string> tempMap;

    for (int i = 0; i < 20; ++i)
    {
        tempMap.insert(std::make_pair(i, std::to_string(i)));
    }

    // 删除元素
    std::map<int, std::string>::iterator iter;
    for (iter = tempMap.begin(); iter != tempMap.end(); )
    {
        if (iter->first == 5 || iter->first ==19)
        {
            iter = tempMap.erase(iter);
        }
        else
        {
            ++iter;
        }

    }

    PrintMap(tempMap);

}

1.2 第二种方式

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

void PrintMap(const std::map<int, std::string>& in_map)
{
    std::map<int, std::string>::const_iterator iter;
    for (iter = in_map.begin(); iter != in_map.end(); iter++)
    {
        std::cout << iter->first << " " << iter->second << std::endl;
    }
}

int main()
{
    std::map<int, std::string> tempMap;

    for (int i = 0; i < 20; ++i)
    {
        tempMap.insert(std::make_pair(i, std::to_string(i)));
    }

    // 删除元素
    std::map<int, std::string>::iterator iter;
    for (iter = tempMap.begin(); iter != tempMap.end(); )
    {
        if (iter->first == 5 || iter->first == 19)
        {
            tempMap.erase(iter++);
        }
        else
        {
            ++iter;
        }

    }

    PrintMap(tempMap);
}

2 while循环遍历std::map删除指定元素

2.1 第一种方式

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

void PrintMap(const std::map<int, std::string>& in_map)
{
    std::map<int, std::string>::const_iterator iter;
    for (iter = in_map.begin(); iter != in_map.end(); iter++)
    {
        std::cout << iter->first << " " << iter->second << std::endl;
    }
}

int main()
{
    std::map<int, std::string> tempMap;

    for (int i = 0; i < 20; ++i)
    {
        tempMap.insert(std::make_pair(i, std::to_string(i)));
    }

    // 删除元素
    std::map<int, std::string>::iterator iter = tempMap.begin();

    while (iter != tempMap.end())
    {
        if (iter->first == 5 || iter->first == 6)
        {
            iter = tempMap.erase(iter);
        }
        else
        {
            iter++;
        }
    }

    PrintMap(tempMap);

}

2.2 第二种方式

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

void PrintMap(const std::map<int, std::string>& in_map)
{
    std::map<int, std::string>::const_iterator iter;
    for (iter = in_map.begin(); iter != in_map.end(); iter++)
    {
        std::cout << iter->first << " " << iter->second << std::endl;
    }
}

int main()
{
    std::map<int, std::string> tempMap;

    for (int i = 0; i < 20; ++i)
    {
        tempMap.insert(std::make_pair(i, std::to_string(i)));
    }

    // 删除元素
    std::map<int, std::string>::iterator iter = tempMap.begin();

    while (iter != tempMap.end())
    {
        if (iter->first == 5 || iter->first == 6)
        {
            tempMap.erase(iter++);
        }
        else
        {
            iter++;
        }
    }

    PrintMap(tempMap);

}

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:C++STL容器 – std::map删除指定元素

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

发布于:2022年02月25日 16:42:43

修改于:2023年06月26日 20:35:11

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

文章末尾
上一篇
C++STL容器 – std::vector容器修改、元素操作总结 push_back,emplace_back,emplace,insert,erase,pop_back,clear,resize,swap
C++
下一篇
计算机图形学 - 3D数学入门之坐标系
3D数学基础
当前分类随机文章推荐

发表评论

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

关注我们的公众号

微信公众号