3

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);
sehe
  • 374,641
  • 47
  • 450
  • 633
user3842408
  • 89
  • 1
  • 10
  • I found a work around, but not sure it's the best way. http://pastebin.com/s2HH2UgS – user3842408 May 22 '15 at 22:22
  • That actually made it clear. I edited the confusing title. You have no problem "querying an array of values". Also, iterators work. But not once invalidated. See my new answer. – sehe May 22 '15 at 22:34

1 Answers1

1

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;
}
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633