vector insert() function in C++ STL
std::vector::insert() is a built-in function in C++ STL which inserts new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted.
Time Complexity – Linear O(N)
- Syntax:
vector_name.insert (position, val)
- Parameter:The function accepts two parameters specified as below:
- position – It specifies the iterator which points to the position where the insertion is to be done.
- val – It specifies the value to be inserted.
- Syntax:
vector_name.insert(position, size, val)
- Parameter:The function accepts three parameters specified as below:
- position – It specifies the iterator which points to the position where the insertion is to be done.
- size – It specifies the number of times a val is to be inserted at the specified position.
- val – It specifies the value to be inserted.
- Syntax:
vector_name.insert(position, iterator1, iterator2)
- Parameter:The function accepts three parameters specified as below:
- position – It specifies the position at which insertion is to be done in vector.
- iterator1 – It specifies the starting position from which the elements are to be inserted
- iterator2 – It specifies the ending position till which elements are to be inserted