1️、基本清空 std::map
使用 clear()
可以删除 map
中的所有元素,销毁每个元素:
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap;
myMap[1] = "one";
myMap[2] = "two";
std::cout << "Before clear, size: " << myMap.size() << std::endl;
// 清空 map
myMap.clear();
std::cout << "After clear, size: " << myMap.size() << std::endl;
return 0;
}
输出:
Before clear, size: 2
After clear, size: 0
注意:clear()
只是删除节点,不一定释放底层内存池分配的所有内存(STL 可能保留内存用于以后复用)。
2️、使用 swap 彻底释放内存
为了让 STL 容器释放所有内存,可以和一个空 map 交换:
std::map<int, std::string>().swap(myMap);
等价于:
std::map<int, std::string> emptyMap;
myMap.swap(emptyMap); // 将 myMap 与空 map 交换
- 优点:保证底层内存释放
- 适合大 map 释放内存,避免内存泄漏
3️、map 中存储指针类型的对象
如果 map
中是指针类型,需要先释放指针指向的内存,否则会泄漏:
#include <map>
#include <string>
#include <iostream>
int main() {
std::map<int, std::string*> myMap;
myMap[1] = new std::string("one");
myMap[2] = new std::string("two");
// 手动释放指针
for (auto& pair : myMap) {
delete pair.second;
}
myMap.clear(); // 删除节点
// 或者彻底释放内存
std::map<int, std::string*>().swap(myMap);
std::cout << "Map cleared and memory released" << std::endl;
}
4️、总结
操作 | 内存释放效果 | 适用场景 |
---|---|---|
myMap.clear() |
删除元素,可能不释放底层节点内存 | 小型 map 或可重复使用的 map |
std::map<…>().swap(myMap) |
删除元素,释放底层内存 | 大型 map,彻底释放内存 |
对指针类型元素手动 delete |
释放指针对象占用内存 | map 存储动态分配对象 |
5、扩展应用示例-模板化函数 free_map_memory
内存释放
下面是一个 模板化函数 free_map_memory
,能自动处理 指针和非指针类型 map 的完全释放,被封装成可复用工具函数,可以直接应用到开发项目中,大家可根据自己的需求进行更改。
示例1,常规版本
#include <map>
#include <type_traits>
#include <memory> // std::addressof
#include <utility> // std::swap
#include <iostream>
template <typename MapType>
void free_map_memory(MapType& m) {
using ValueType = typename MapType::mapped_type;
// 如果 ValueType 是指针类型,先 delete 指针
if constexpr (std::is_pointer_v<ValueType>) {
for (auto& kv : m) {
delete kv.second;
}
}
// 清空 map 元素
m.clear();
// 彻底释放底层内存
MapType().swap(m);
}
// ------------------- 示例 -------------------
struct MyData {
int x;
MyData(int v) : x(v) {}
};
int main() {
std::map<int, int> normalMap;
normalMap[1] = 100;
normalMap[2] = 200;
std::map<int, MyData*> pointerMap;
pointerMap[1] = new MyData(10);
pointerMap[2] = new MyData(20);
std::cout << "Before free, normalMap size: " << normalMap.size() << std::endl;
std::cout << "Before free, pointerMap size: " << pointerMap.size() << std::endl;
free_map_memory(normalMap);
free_map_memory(pointerMap);
std::cout << "After free, normalMap size: " << normalMap.size() << std::endl;
std::cout << "After free, pointerMap size: " << pointerMap.size() << std::endl;
return 0;
}
功能特点
- 自动识别值类型是否为指针:使用
std::is_pointer_v
。 - 安全释放指针类型对象:自动
delete
。 - 彻底释放 map 内存:使用
swap
与临时空 map 交换。 - 通用:支持任何
std::map<Key, Value>
类型,包括自定义结构体指针。
示例2,智能指针版本
加强版本能自动识别并处理以下几类 map
:
- 值类型为普通对象
- 值类型为原始指针
- 值类型为
std::unique_ptr
或std::shared_ptr
模板函数会自动释放内容并彻底回收 map 内存。
#include <map>
#include <memory>
#include <type_traits>
#include <utility>
#include <iostream>
template <typename MapType>
void free_map_memory(MapType& m) {
using ValueType = typename MapType::mapped_type;
// 原始指针类型
if constexpr (std::is_pointer_v<ValueType>) {
for (auto& kv : m) {
delete kv.second;
}
}
// unique_ptr 或 shared_ptr 类型
else if constexpr (std::is_same_v<ValueType, std::unique_ptr<typename ValueType::element_type>> ||
std::is_same_v<ValueType, std::shared_ptr<typename ValueType::element_type>>) {
// 智能指针自动释放,无需手动 delete
}
// 普通对象类型,无需特殊处理
// 清空 map 元素
m.clear();
// 彻底释放底层内存
MapType().swap(m);
}
// ------------------- 示例 -------------------
struct MyData {
int x;
MyData(int v) : x(v) {}
};
int main() {
// 普通对象 map
std::map<int, int> normalMap{{1,100},{2,200}};
// 原始指针 map
std::map<int, MyData*> pointerMap;
pointerMap[1] = new MyData(10);
pointerMap[2] = new MyData(20);
// unique_ptr map
std::map<int, std::unique_ptr<MyData>> uniquePtrMap;
uniquePtrMap[1] = std::make_unique<MyData>(30);
uniquePtrMap[2] = std::make_unique<MyData>(40);
// shared_ptr map
std::map<int, std::shared_ptr<MyData>> sharedPtrMap;
sharedPtrMap[1] = std::make_shared<MyData>(50);
sharedPtrMap[2] = std::make_shared<MyData>(60);
free_map_memory(normalMap);
free_map_memory(pointerMap);
free_map_memory(uniquePtrMap);
free_map_memory(sharedPtrMap);
std::cout << "All maps freed successfully." << std::endl;
return 0;
}
功能特点
- 自动区分普通对象 / 原始指针 / 智能指针
- 原始指针自动
delete
- 智能指针无需手动释放
- 彻底回收 map 内存,避免底层内存占用