本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++STL容器 – std::map删除指定元素
原文链接:https://www.stubbornhuang.com/1985/
发布于:2022年02月25日 16:42:43
修改于:2022年02月25日 16:42:43

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(); ++iter)
{
if (iter->first == 5)
{
iter = tempMap.erase(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)
{
tempMap.erase(iter++);
}
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 = tempMap.erase(iter);
}
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)
{
tempMap.erase(iter++);
}
iter++;
}
PrintMap(tempMap);
}
当前分类随机文章推荐
- C++11/std::shared_ptr - 循环引用问题 阅读2657次,点赞0次
- C++ - 使用C++标准库过滤Windows文件名中的非法字符 阅读2976次,点赞1次
- C++ - 在Windows/Linux上创建单级目录以及多级目录的跨平台方法 阅读40次,点赞0次
- C++ - Windows/Linux生成uuid(通用唯一识别码) 阅读101次,点赞0次
- C++11 - 构建一个符合实际应用要求的线程池 阅读483次,点赞0次
- C++ - 字节数组byte[]或者unsigned char[]与long的相互转换 阅读146次,点赞0次
- C++ - 求解std::vector
中topk数值以及topk数值对应的索引 阅读436次,点赞0次 - C++11 - std::bind简要介绍以及可绑定函数的几种形式总结 阅读2908次,点赞3次
- CMake - 设置Debug或者Release编译模式 阅读70次,点赞0次
- C++ - 格式化json字符串,方便展示json字符串的层次结构 阅读550次,点赞0次
全站随机文章推荐
- OpenCV - 将图片转换为深度学习模型输入格式,BGR通道转RGB,图片归一化,HWC转CHW 阅读1409次,点赞0次
- Modern OpenGL从零开始 - 多个帧缓存Framebuffer绘制到同一个铺满屏幕四边形Quad上 阅读1916次,点赞1次
- 资源分享 - Foundations of Physically Based Modeling and Animation 英文PDF下载 阅读2100次,点赞0次
- 资源分享 - Mathematics for 3D Game Programming and Computer Graphics, Third Edition英文高清PDF下载 阅读1719次,点赞0次
- 资源分享 - Vector Analysis for Computer Graphics , Second Edition 英文高清PDF下载 阅读259次,点赞0次
- Windows - 使用类的成员函数作为Win32窗口消息回调处理函数WindowProc 阅读34次,点赞0次
- 资源分享 - An Integrated Introduction to Computer Graphics and Geometric Modeling 英文高清PDF下载 阅读698次,点赞0次
- 资源分享 - Real-Time Rendering, Fourth Edition Pdf英文原版下载 阅读50835次,点赞20次
- ThreeJS - 动态更换fbx模型的某个子Mesh现有的纹理贴图为指定的纹理贴图 阅读1328次,点赞1次
- Python - list/numpy/pytorch tensor相互转换 阅读799次,点赞0次
评论
144