vector_pro::insert¶
size_type insert(size_type position, const value_type& val);
iterator_pro<value_type> insert(iterator_pro<value_type> position, const value_type& val);
iterator_pro<value_type> insert(iterator_pro<value_type> position, size_type num, const value_type& val);
iterator_pro<value_type> insert(iterator_pro<value_type> position, const_iterator_pro<value_type> from, const_iterator_pro<value_type> exclude_to);
iterator_pro<value_type> insert(iterator_pro<value_type> position, const std::initializer_list<value_type>& li);
const_iterator_pro<value_type> insert(const_iterator_pro<value_type> position, const value_type& val);
const_iterator_pro<value_type> insert(const_iterator_pro<value_type> position, size_type num, const value_type& val);
const_iterator_pro<value_type> insert(const_iterator_pro<value_type> position, const_iterator_pro<value_type> from, const_iterator_pro<value_type> exclude_to);
const_iterator_pro<value_type> insert(const_iterator_pro<value_type> position, const std::initializer_list<value_type>& li);
Insert elements¶
The vector is extended by inserting new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted.
This causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity.
Because vectors use an array as their underlying storage, inserting elements in positions other than the vector end causes the container to relocate all the elements that were after position to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such as list or forward_list).
Parameters¶
position
Position in the container where the new elements are inserted.
Can besize_type/iterator_pro/const_iterator_pro.val
Value to be copied to the inserted elements.num
Number of elements to insert. Each element is initialized to a copy ofval.from,exclude_to
The range of elements we are going to insert.
Can besize_type/iterator_pro/const_iterator_pro.
The range includes all the elements betweenfromandexclude_to, including the element pointed byfrombut not the one pointed byexclude_to.li
An initializer_list object. Copies of these elements are inserted atposition(in the same order).
Return value¶
The return value will point to the first of the newly inserted elements.
Type will be same as the position. ( size_type / iterator_pro / const_iterator_pro )
Example¶
// inserting into a vector_pro
#include <iostream>
#include "vector_pro.h"
/**
* Output:
* myvector contains: 501 502 503 300 300 400 400 200 100 100 100
*/
int main ()
{
vector_pro<int> myvector (3,100);
iterator_pro<int> it;
it = myvector.begin();
it = myvector.insert ( it , 200 );
myvector.insert (it,2,300);
// "it" no longer valid, get a new one:
it = myvector.begin();
vector_pro<int> anothervector (2,400);
myvector.insert (it+2,anothervector.begin(),anothervector.end());
std::initializer_list<int> myarray = { 501,502,503 };
myvector.insert (myvector.begin(), myarray);
std::cout << "myvector contains:";
for (it=myvector.begin(); it != myvector.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
Complexity¶
Linear on the number of elements inserted (copy/move construction) plus the number of elements after position (moving).