Reputation: 89
I started using the ptree and the json parser from boost to save some quick information. The problem is that I only need to save some URI, so I don't care about a key. Now I want to find a certain value and remove it. How can I do this
{
"MyArray":
[
"value1",
"value2"
]
}
I can't use find()
and iterators don't seem to work.
for (it ; it != myptree.end(); ++it)
if ((*it).second.data.compare(strValueImSearching) != 0)
// the previous line is not valid from .data
myptree.erase(it);
Upvotes: 3
Views: 987
Reputation: 393174
Ah. Lightbulb moment.
You cannot iterate a collection that's changing like that, because the erase
calls invalidate the current iterator.
See here for iterator invalidation rules: Iterator invalidation rules
Instead, indeed use stable iterators:
while (it != myptree.end()) {
if ((*it).second.data.compare(strValueImSearching) != 0)
it = myptree.erase(it);
else
++it;
}
Upvotes: 1