py::builtins::list::insert¶
void insert(std::size_t index, const_reference element); // 1
void insert(std::size_t index, value_type&& element); // 2
template <class input_it>
void insert(std::size_t index, input_it first, input_it last); // 3
Inserts given element at given index.
Same as (1).
Inserts given element range at given index.
Parameters¶
element: The element to insert.
first: Iterator to the first element to insert.
last: Iterator to one past the last element to insert.
Complexity¶
Constant plus linear in the distance between
indexand the end if the insertion causes the memory to be reallocated, constant plus linear in the size of the list otherwise.Same as (1).
Linear in the distance between
firstandlastplus linear in the distance betweenindexand the end.
Example¶
#include <pypp/builtins/list.hpp>
#include <pypp/builtins/print.hpp>
int main()
{
py::list<int> list = {1, 3, 5};
py::print(list);
list.insert(0, 0);
list.insert(2, 2);
list.insert(-1, 4); // insert with negative index
py::print(list);
}
Output:
{1, 3, 5}
{0, 1, 2, 3, 4, 5}