谁在替换值时删除以前的 nlohmann json 对象资源?
Who deletes previous nlohmann json object resources when value is replaced?
我有一把钥匙,我想用另一个 json 对象更改钥匙的值。
json newjs = ...;
json tempjs = ...;
newjs["key"] = tempjs["key"];
之前newjs["key"]
中的数据会怎样?
nlohmann class 会自动销毁还是内存泄漏?
或者我是否需要先手动擦除密钥并按上述方式分配?
在内部,它由“ordered_map
:保留插入顺序的最小类地图容器”保存。
这个ordered_map
中使用的实际标准容器是std::vector<std::pair<const Key, T>, Allocator>
,你所做的分配是通过
执行的
T& operator[](const Key& key)
{
return emplace(key, T{}).first->second;
}
其中 emplace
定义为:
std::pair<iterator, bool> emplace(const key_type& key, T&& t)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (it->first == key)
{
return {it, false};
}
}
Container::emplace_back(key, t);
return {--this->end(), true};
}
这意味着 operator[]
试图 emplace
一个默认初始化的 T
进入内部映射。如果地图中不存在 key
,它将成功,否则将失败。
无论如何,当 emplace
returns 时,映射中将有一个 T
,它是 T
返回的 T
的引用16=] 然后你复制分配给它。
这是一个“正常”的复制分配,不会发生泄漏。
我有一把钥匙,我想用另一个 json 对象更改钥匙的值。
json newjs = ...;
json tempjs = ...;
newjs["key"] = tempjs["key"];
之前newjs["key"]
中的数据会怎样?
nlohmann class 会自动销毁还是内存泄漏?
或者我是否需要先手动擦除密钥并按上述方式分配?
在内部,它由“ordered_map
:保留插入顺序的最小类地图容器”保存。
这个ordered_map
中使用的实际标准容器是std::vector<std::pair<const Key, T>, Allocator>
,你所做的分配是通过
T& operator[](const Key& key)
{
return emplace(key, T{}).first->second;
}
其中 emplace
定义为:
std::pair<iterator, bool> emplace(const key_type& key, T&& t)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (it->first == key)
{
return {it, false};
}
}
Container::emplace_back(key, t);
return {--this->end(), true};
}
这意味着 operator[]
试图 emplace
一个默认初始化的 T
进入内部映射。如果地图中不存在 key
,它将成功,否则将失败。
无论如何,当 emplace
returns 时,映射中将有一个 T
,它是 T
返回的 T
的引用16=] 然后你复制分配给它。
这是一个“正常”的复制分配,不会发生泄漏。