-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array manipulation: erasing members while looping through the array #1316
Comments
Similiar to std::vector::erase(), you need to use the returned iterator after erasing an element: //! Remove an element of array by iterator.
/*!
\param pos iterator to the element to remove
\pre IsArray() == true && \ref Begin() <= \c pos < \ref End()
\return Iterator following the removed element. If the iterator pos refers to the last element, the End() iterator is returned.
\note Linear time complexity.
*/
ValueIterator Erase(ConstValueIterator pos) {
// ...
} |
Thanks. Did not realise the Erase() worked the same as with a vector 📄 Update: Update #2: |
You increment the iterator twice in case of a successful for (rapidjson::Value::ConstValueIterator lItr = lDocument["randomIds"].Begin();
(lItr != lDocument["randomIds"].End()) && (!lDone); /* ++lItr */ ) //<-- don't increment lItr here
{
if (lIndexCounter == lIndex) {
// Erase and then update the main index counter
lItr = lDocument["randomIds"].Erase(lItr);
--lIndex;
lDone = true;
} else {
++lItr; // <-- increment, if not erased
}
++lIndexCounter;
} |
Thanks, @pah. I still noticed an issue when using erase and set, using only Value* and the nesting was multiple levels deep. But I fixed that by specifically using the RootDocument when erasing. |
Apologies for implying that you might have overlooked this. :-)
Hard to tell, what might have gone wrong. There is another potential issue in the original code: The use of |
So, I've been working on a wrapper for RapidJSON for the company I work at and so far, everyone's been really enjoying it.
Now I've created some tool using that wrapper to quickly update our 15k+ JSON models to a new format. Which mostly works fine, except that I seem to have encountered a bug where RapidJSON allocators seem to lose track when iterating through an array, that is changing size during the iteration.
Here's some test code to illustrate the issue:
The output is this:
As you can clearly see, something goes terribly wrong after the first iteration ...
The first time, Test0 is correctly written to the first element. However, when the 2nd element gets copied and erased, the entire array starts falling apart.
The text was updated successfully, but these errors were encountered: