Skip to content
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

Add missing erase(first, last) function to ordered_map #3109

Merged
merged 7 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions include/nlohmann/ordered_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,14 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,

iterator erase(iterator first, iterator last)
{
auto elements_affected = std::distance(first, last);
const auto offset = std::distance(Container::begin(), first);
const auto elements_affected = std::distance(first, last);

// This is the start situation. We need to delete elements_affected
// elements (3 in this example: e, f, g), and need to return an
// iterator past the last deleted element (h in this example).
// Note that offset is the distance from the start of the vector
// to first. We will need this later.

// [ a, b, c, d, e, f, g, h, i, j ]
// ^ ^
Expand Down Expand Up @@ -149,8 +152,10 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
// ^ ^
// first last

// first is now pointing past the last deleted element
return first;
// first is now pointing past the last deleted element, but we cannot
// use this iterator got invalidated by the resize call. Instead, we
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
// can return begin() + offset.
return Container::begin() + offset;
}

size_type count(const Key& key) const
Expand Down
11 changes: 8 additions & 3 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17499,11 +17499,14 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,

iterator erase(iterator first, iterator last)
{
auto elements_affected = std::distance(first, last);
const auto offset = std::distance(Container::begin(), first);
const auto elements_affected = std::distance(first, last);

// This is the start situation. We need to delete elements_affected
// elements (3 in this example: e, f, g), and need to return an
// iterator past the last deleted element (h in this example).
// Note that offset is the distance from the start of the vector
// to first. We will need this later.

// [ a, b, c, d, e, f, g, h, i, j ]
// ^ ^
Expand Down Expand Up @@ -17536,8 +17539,10 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
// ^ ^
// first last

// first is now pointing past the last deleted element
return first;
// first is now pointing past the last deleted element, but we cannot
// use this iterator got invalidated by the resize call. Instead, we
// can return begin() + offset.
return Container::begin() + offset;
}

size_type count(const Key& key) const
Expand Down