added erase function for arrays
This commit is contained in:
parent
9ea3dd9b60
commit
84195daf28
3 changed files with 121 additions and 2 deletions
19
src/json.hpp
19
src/json.hpp
|
|
@ -1190,7 +1190,7 @@ class basic_json
|
|||
/// remove element from an object given a key
|
||||
inline size_type erase(const typename object_t::key_type& key)
|
||||
{
|
||||
// at only works for objects
|
||||
// this erase only works for objects
|
||||
if (m_type != value_t::object)
|
||||
{
|
||||
throw std::runtime_error("cannot use erase with " + type_name());
|
||||
|
|
@ -1199,6 +1199,23 @@ class basic_json
|
|||
return m_value.object->erase(key);
|
||||
}
|
||||
|
||||
/// remove element from an array given an index
|
||||
inline void erase(const size_type pos)
|
||||
{
|
||||
// this erase only works for arrays
|
||||
if (m_type != value_t::array)
|
||||
{
|
||||
throw std::runtime_error("cannot use erase with " + type_name());
|
||||
}
|
||||
|
||||
if (pos >= size())
|
||||
{
|
||||
throw std::out_of_range("index out of range");
|
||||
}
|
||||
|
||||
m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(pos));
|
||||
}
|
||||
|
||||
/// find an element in an object
|
||||
inline iterator find(typename object_t::key_type key)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1190,7 +1190,7 @@ class basic_json
|
|||
/// remove element from an object given a key
|
||||
inline size_type erase(const typename object_t::key_type& key)
|
||||
{
|
||||
// at only works for objects
|
||||
// this erase only works for objects
|
||||
if (m_type != value_t::object)
|
||||
{
|
||||
throw std::runtime_error("cannot use erase with " + type_name());
|
||||
|
|
@ -1199,6 +1199,23 @@ class basic_json
|
|||
return m_value.object->erase(key);
|
||||
}
|
||||
|
||||
/// remove element from an array given an index
|
||||
inline void erase(const size_type pos)
|
||||
{
|
||||
// this erase only works for arrays
|
||||
if (m_type != value_t::array)
|
||||
{
|
||||
throw std::runtime_error("cannot use erase with " + type_name());
|
||||
}
|
||||
|
||||
if (pos >= size())
|
||||
{
|
||||
throw std::out_of_range("index out of range");
|
||||
}
|
||||
|
||||
m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(pos));
|
||||
}
|
||||
|
||||
/// find an element in an object
|
||||
inline iterator find(typename object_t::key_type key)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue