本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++STL容器 – std::map删除指定元素
原文链接:https://www.stubbornhuang.com/1985/
发布于:2022年02月25日 16:42:43
修改于:2023年02月20日 16:00:24
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);
}
当前分类随机文章推荐
- C++STL容器 – std::vector容器修改、元素操作总结 push_back,emplace_back,emplace,insert,erase,pop_back,clear,resize,swap 阅读1044次,点赞1次
- C++ - 我在项目实际开发中用到的第三方库/开源项目,涵盖网络、加密解密、GUI、网络、音视频、图片等等 阅读260次,点赞0次
- C++ – 字节数组byte[]或者unsigned char[]与float的相互转换 阅读2021次,点赞0次
- C++ - 使用标准库实现事件和委托,信号和槽机制 阅读410次,点赞0次
- C++11 - 使用std::thread::join()/std::thread::detach()方法需要注意的点 阅读2901次,点赞0次
- C++ - sleep睡眠函数总结 阅读1123次,点赞0次
- C++ - 使用标准库std::use_facet和std::codecvt进行跨平台gbk与utf8字符集转换 阅读268次,点赞0次
- C++11 - 使用std::chrono计算程序、函数运行时间 阅读2780次,点赞0次
- C++11 - 使用std::thread在类内部以成员函数作为多线程函数执行异步操作 阅读2618次,点赞0次
- 计算几何 - C++计算两个二维向量的夹角 阅读4347次,点赞3次
全站随机文章推荐
- 书籍翻译 – Fundamentals of Computer Graphics, Fourth Edition,第4章 Ray Tracing中文翻译 阅读1974次,点赞7次
- 资源分享 - Mathematics for Computer Graphics , Fifth Edition 英文高清PDF下载 阅读1382次,点赞0次
- C++ - 随机洗牌算法,std::random_shuffle和std::shuffle 阅读1596次,点赞2次
- NCNN - 使用onnx2ncnn工具将onnx转换ncnn模型出现Shape not supported yet!,Unknown data type 0错误 阅读212次,点赞0次
- 资源分享 - Learning Vulkan 英文高清PDF下载 阅读2173次,点赞0次
- Duilib - CDuiString转换为std::string 阅读2008次,点赞0次
- 姿态估计之Openpose-Body25数据集骨骼关节keypoint标注对应 阅读8419次,点赞5次
- C++ - 使用C++标准库过滤Windows文件名中的非法字符 阅读4609次,点赞1次
- Pytorch - 使用torchsummary/torchsummaryX/torchinfo库打印模型结构、输出维度和参数信息 阅读1562次,点赞1次
- 资源分享 - Computational Geometry - Algorithms and Applications, First Edition 英文高清PDF下载 阅读1769次,点赞0次
评论
169