vector::insert function


The insert function of vector allows you to insert data in any position of the vector.There are five overloaded version of insert function in vector.The five function is given below.

T : type of the data.

1 iterator insert(const_iterator pos, const T& x);
2 iterator insert ( iterator pos, size_t n , const T& val );
3 iterator template<typename it> insert(const_iterator pos , it first, it second) ;
4 iterator insert( const_iterator pos , initializer_list l ) ;(C++11)
5 iterator insert (const_iterator pos , T&& val) ; (C++11)

How each of these functions insert the data in the vector is explained in detail below,but note they have one common purpose:to insert data in the vector container.A pictorial representation of how the storage changes with data insertion is shown below.

vector insert function



iterator insert(const_iterator pos, const T& x);

This function insert a data in the vector before the position provided as the first argument.

Parameters:

pos – The position in the vector before which the data is to be inserted.

val – The value to be inserted in the vector.

Return type

iterator – This iterator points to the position at which the data was inserted.

Note this function is called only when you provide the second argument as either a reference or a variable,if you provide a literal the 5th insert function will be called.

Code example:

int val=99999 , &ref=val=val ;

vector<int> vec={ 23 , 45 ,56 } ;

vec.insert( vec.being()+1 , ref ) ; ///insert ‘ref’ at the 2nd position of the vector

//outputting the content
for(auto elem:vec)
{
cout<< elem << “,” ;
}

Output

23 , 99999 , 45 , 56

To test the returned iterator position,look at the code below.

Code example:

int val=99999 , &ref=val=val ;

vector<int> vec={ 23 , 45 ,56 } ;

vector<int>::iterator it=vec.insert( vec.being()+1 , ref ) ;

cout<< *it << endl vec[1] ; //the output is same

cout<< &(*it) << ” , ” &vec[1] ; //the address of the storage pointed by vec[1]and ‘it’ iterator

Output:

99999 , 99999
0x1d16b4 , 0x1d16b4

it‘ point to vec[1] position.






iterator insert ( iterator pos, size_t n , const T& val );

This function will insert a specified number of copies of data at a given position in the vector.

Parameters:

pos – The position in the vector before which the data is to be inserted.

n – An unsigned int type ,which determine the number of times the data is to be inserted.

val – The value to be inserted in the vector.

Return type

iterator – This iterator points to the position at which the data was inserted.

Code example:

vector<int> vec={2,56,3} ;

vec.insert( vec.being()+1 , 3 , 777 ); //insert 777,3 times at vec[1]

///outputting the value
for(auto elem:vec)
{
cout<< elem << “,” ;
}

///testing the iterator returned by this function
vector<int> ::iterator vecIt=vec.insert( vec.being()+2 , 2 , 787 ) ;

cout<< vec[2] << ” , ” << &(*vecIt) ;

Output:

2 , 777 , 777 , 777 , 56 , 3
0xa71734 , 0xa71734



iterator template<typename it> insert(const_iterator pos , it first, it second)

This function also insert a number of copies of data in the specified position.However,this function is called only when you provide the second and third argument as an iterator.These iterators must denote certain range in another container from where the data can be taken and inserted into the vector.The data taken from the other container range from [first last) .

Parameters:

pos – The position in the vector before which the data is to be inserted.

first – An iterator type denoting the beginning of the range from which the data is to be taken.

second -An iterator type denoting the end of the range.

Return type

iterator – This iterator points to the position at which the data was inserted.

Code example:

vector<int> vec={4,5} ,
vecNew={ 99 ,999 };

vec.insert( vec.begin() , vecNew.begin() , vecNew.end( ) );

//outputting the data
for(auto elem:vecNew )
{
cout<< elem << ” “;
}

Output:

99 , 999 , 4 , 5

Here is another example where the data is taken from the array and inserted into the vector.

Code example:

vector<int> vec={4,5} ;

int arr[ ]={ 2 , 45 } ;

vec.insert( vec.begin() , arr , arr+1 );

//outputting the data
for(auto elem:vecNew )
{
cout<< elem << ” “;
}

Output,

2 , 4 , 5

The element pointed by the third pointer:’arr+1‘ is not inserted into the vector.


 


Stop wasting time,earn money($$$) from your website-Join Now!


iterator insert( const_iterator pos , initializer_list l ) ;(C++11)

The function will insert data taken from the intializer_list into the specified position.This function is added in C++11.

Parameters:

pos – The position in the vector before which the data is to be inserted.This argument is of const_iterator type.

l – An initializer_list object.

Return type

iterator – This iterator points to the position at which the data was inserted.

Code example:

initializer_list<string> il={ “New” , “string” };

vec<string> vecSt={ “shoe” };

vecSt.insert( vecSt.begin( ) , il ) ;

///outputting the data
for(auto elem:vecSt)
{
cout<< elem << ” , “;
}

Output:

New , string , shoe



iterator insert (const_iterator pos , T&& val) ; (C++11)

The function insert given rvalue in the specified position.This function is added in C++11.

Parameters:

pos – The position in the vector before which the data is to be inserted.This argument is of const_iterator type.

val – A rvalue.

Return type

iterator – This iterator points to the position at which the data was inserted.

Code example

vector<int > vec={34 , 56 } ;

vec.insert( vec.being() , 888 ) ;

//outputting the data
for(auto elem:vec)
{
cout<< elem << ” , ” ;
}

Output

888 , 34 , 56

Note,Whenever you supply a literal data type this function is always called,the first overloaded version discussed at the first section is never called.