C++11 vector::emplace function


The C++11 vector emplace function inserts object at a position before the specified iterator in a vector.This function was added as part of C++11 features.

template<typename Arg>iterator emplace(const_iterator pos , Arg… );(C++11)

Parameters:
pos – An iterator denoting the position before which the object must be inserted.

Return type
iterator – It points to the inserted data.

In using this function the data to be inserted is constructed in the position just before the iterator passed as the first argument.If the type of the vector is a user-defined type :class or structure, you can directly passed the data which the constructor of that type accept.In doing so the object of the class or structure is constructed in the required position(a code example is given to illustrates this concept later).

Code example: when the type is built-in type.

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

vec.emplace( vec.begin( ) , 678 ) ; //inserts 678 before vec.begin( )

for(auto elem:vec)
{
cout<< elem << ” ” ;
}

Output

678 2 3

Code example: Checking the iterator returned by emplace.

vector<int>::vecIt =vec.emplace( vec.begin( )+1 , 999 ) ; //inserts 999 befor ‘vec.begin() +1’ (at vec[1] position)

cout<< *vecIt ;

Output

999

vecIt points to 999-the inserted data.




Code example: When the type is a user-defined data type
(code explanation is given below).

class test
{
 int i ;
 string st ;

public:
 test(int ii , string s): i(ii) , st(s) { }

 string getStr( ) const { return st; }
 int getInt( ) const { return i; }

 ~test( ) { }
};

int main( )
{
vector<test> vectest={ test(34 ,” testing emplace”) , test(90 , ” function”) } ; //Object initialization

vectest.emplace( vectest.begin() , test(10 , ” Happy”) ) ; //work fine

vectest.emplace( vectest.end( ) , 100 , ” call” ) ; //work fine

vectest.emplace( vectest.begin( )+1 , 89 ) ; //error cannot construct ‘test’ object from an integer data

vectest.emplace( vectest.begin()+1 , “World” , 789 ); //error in argument type arrangement

for(auto elem: vectest )
{
cout<< elem.getInt( ) << elem.getStr( ) << endl ;
}

cin.get( ) ;
return 0 ;
}

Output

10 Happy
34 testing emplace
90 function
100 call

In the first emplace call a test object is passed which is inserted at the beginning of the vector.

In the second emplace call we have not passed a test object ,instead we have passed an integer and a string as second and third argument.But the code seem to work fine why? If we look at the constructor of test it accepts an integer and a string data.Although we have not passed a test object explicitly the emplace function will create one by using the second and third argument to call the constructor of test.The emplace function creates a test object automatically for us and this object is inserted at the end of the container.

In the third emplace call we have passed just an integer value which is not sufficient to call the test constructor,which accepts int and string data.Hence in this case emplace cannot generate a test object.So we get an error.

In the fourth emplace call the second argument is string and the third argument is int type.The constructor of test however accepts int as first argument and string as second argument.There is a contradiction in the type here so in this case also emplace cannot call the test constructor and hence it’s object cannot be created.So we get an error.



Related links

->C++ emplace_back vector

->C++ push_back vector

->C++ begin vector

->C++ data vector