-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
erase multiple elements from a json object #884
Comments
You can't use http://en.cppreference.com/w/cpp/algorithm/remove
The fact that it even compiles is a bit odd. There must be something about the extra stuff in the class for it being multiple types at once that makes it compile. |
Ah, I think this is because the raw |
I agree with @gregmarr that there seems to be no way to achieve this with |
Good news: you can use the solution from https://stackoverflow.com/a/800984/266378: auto iter = j.begin();
for(; iter != j.end(); ) {
if (iter.value().is_null()) {
j.erase(iter++);
} else {
++iter;
}
} |
I think this slight change would be more idiomatic, and potentially safer.
|
Thx. It is not so obvious that it's implemented as map. Then it's clean it will not work with remove_if. |
@best9541 no, the |
yes. auto iter = j.cbegin();
for (; iter != j.cend();) {
if (iter.value().is_null()) {
iter = j.erase(iter);
}
else {
++iter;
}
} |
Anyway to use the new std::erase_if ( Oh I see not quite like that, but you can use |
This should work but I haven't tested it: std::erase_if(j.get_ref<json::object_t &>(), pred); |
Thanks, for me turns out I actually wanted |
Hmm, I'm having trouble getting the proposed solution to work. According to the docs, iterators after the one that's erased are invalidated so erasing in a loop of any kind seems impossible. |
The proposed solution avoids that problem. Why is that not working for you? |
Bug Report
I want to erase several elements of a json object having a specific criteria.
Therefore I use json::erase with std::remove_if and a lambda function:
The first output is
After removal of optional elements (null), the output is
It seems that only the values having 'null' are deleted, but not the associating key.
I would have expected that the key & value get's deleted to get the following output:
The text was updated successfully, but these errors were encountered: