Boost ptree动态修改值数组

boost ptree modify an array of values on the fly

本文关键字:数组 修改 动态 ptree Boost      更新时间:2023-10-16

我开始使用boost中的ptree和json解析器来保存一些快速信息。问题是我只需要保存一些URI,所以我不关心键。现在我想找到一个特定的值,然后把它去掉。我该怎么做呢

{
    "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调用使当前迭代器失效。

查看这里的迭代器失效规则:

应该使用稳定迭代器:

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