gpt4 book ai didi

c++ - boost ptree 即时修改值数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:50:14 25 4
gpt4 key购买 nike

我开始使用 boost 的 ptree 和 json 解析器来保存一些快速信息。问题是我只需要保存一些 URI,所以我不关心 key 。现在我想找到某个值并将其删除。我该怎么做

{
"MyArray":
[
"value1",
"value2"
]
}

我不能使用 find() 并且迭代器似乎不起作用。

for (it ; it != myptree.end();  ++it)
if ((*it).second.data.compare(strValueImSearching) != 0)
// the previous line is not valid from .data
myptree.erase(it);

最佳答案

啊。灯泡时刻。

您不能迭代像那样变化的集合,因为 erase 调用会使当前迭代器无效。

See here for iterator invalidation rules: Iterator invalidation rules

相反,确实使用稳定的迭代器:

while (it != myptree.end()) {
if ((*it).second.data.compare(strValueImSearching) != 0)
it = myptree.erase(it);
else
++it;
}

关于c++ - boost ptree 即时修改值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30406015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com