added another insert function
This commit is contained in:
parent
092bf39f53
commit
1c8d5dc211
6 changed files with 129 additions and 0 deletions
src
39
src/json.hpp
39
src/json.hpp
|
@ -3876,6 +3876,45 @@ class basic_json
|
|||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief inserts elements
|
||||
|
||||
Inserts elements from initializer list @a ilist before iterator @a pos.
|
||||
|
||||
@param[in] pos iterator before which the content will be inserted; may be
|
||||
the end() iterator
|
||||
@param[in] ilist initializer list to insert the values from
|
||||
|
||||
@throw std::domain_error if called on JSON values other than arrays
|
||||
@throw std::domain_error if @a pos is not an iterator of *this
|
||||
@return iterator pointing to the first element inserted, or @a pos if
|
||||
`ilist` is empty
|
||||
|
||||
@complexity Linear in `ilist.size()` plus linear in the distance between @a
|
||||
pos and end of the container.
|
||||
|
||||
@liveexample{The example shows how insert is used.,insert__ilist}
|
||||
*/
|
||||
iterator insert(const_iterator pos, std::initializer_list<basic_json> ilist)
|
||||
{
|
||||
// insert only works for arrays
|
||||
if (m_type != value_t::array)
|
||||
{
|
||||
throw std::domain_error("cannot use insert() with " + type_name());
|
||||
}
|
||||
|
||||
// check if iterator pos fits to this JSON value
|
||||
if (pos.m_object != this)
|
||||
{
|
||||
throw std::domain_error("iterator does not fit current value");
|
||||
}
|
||||
|
||||
// insert to array and return iterator
|
||||
iterator result(this);
|
||||
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, ilist);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief exchanges the values
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue