C++11 vector::emplace_back function

The C++ vector emplace_back function like the push_back function append data at the end of the vector.It is added in C++11.The declaration of the function is given below.

template<typename… Args> void emplace_back(Args&&… args);

args – A single argument or list of arguments.

Return type

An example is given below which uses emplace_back function to append data at the end of the vector.

 vector&lt;int&gt; vec ={ 23 , 56 , 89 } ;

vec.emplace_back( 981 ) ;
vec.push_back( 1425 ) ;

for( auto elem:vec )
cout&lt;&lt; elem &lt;&lt; ” ” ;


23 56 89 981 1425

In the code above the push_back and emplace_back perform exactly the same operation;to append data at the end of the vector.

Here is another example where the vector type is a user-defined type: class or structure.

class AA
string ss;


AA(string st):ss(st) { }

string getString( ) const { return ss; }

~AA( ) { }

int main( )
vector&lt;AA&gt; vecA={ AA(“String”) , “Gift” , AA(“New”) }; ///vecA intialization

vecA.push_back( AA(“happy”) ) ; //AA object is appended

vecA.emplace_back( “Crop” ) ; ///work fine

vecA.push_back( “Dome” ) ; ///work fine

for( auto elem:vecA )
cout&lt;&lt; elem.getString() &lt;&lt; ” ” ;

cin.get( ) ;
return 0 ;


String Gift New Happy Dome

In the first push_back(line 18 for PC user) we appended an AA object which is fine.In the emplace_back(line 20 for PC) and the second push_back function call(line 22 for PC) a string data is passed instead of AA object.This is also acceptable because the emplace_back and push_back can create object from the argument passed which is then added into the vector.

If the type of the vector has a constructor which accept more than one argument then,the arguments passed to the emplace_back must match those arguments typw.If the arguments type does not match it cannot call the constructor and hence it cannot create an object of the type.In which case the compiler will give an error.

class BB
int i;
string st;

BB(int ii , string ss ):i(ii),st(ss) { }

~BB( ) { }

vector&lt;BB&gt; vec;

vec.emplace_back( 89 , “New” ); //work fine

vec.emplace_back( 78 ) ; //error! requires second string argument

vec.emplace_back( “Monkey” , 890 ); //error! arguments does not match

Invalidate iterators,pointers and reference if reallocation occurs

This function will invalidate iterators ,pointers and reference referring to the vector only if reallocation occurs.If reallocation does not occur the iterators , pointers and reference remains valid.The reallocation occur only if the size is greater than the capacity of the vector.

Link :C++ vector push_back

vector&lt;int&gt; vec;

vec.reserve( 3 ) ;

vector&lt;int&gt; ::iterator vecIt=vec.begin( ) ;


cout&lt;&lt; *vecIt &lt;&lt; endl ;//iterator is still valid

vec.emplace_back( 4560 );

cout&lt;&lt; *vecIt ; //undefined

Calling emplace_back for three times does not reallocate the storage since a storage for three objects was reserved.But at the fourth emplace_back call reallocation takes place since the size becomes greater than the capacity.And once the reallocation occurs the storage has a new memory address but the iterator vecIt is still pointing to the previous storage and thus it becomes invalid.Hence accessing the storage using vecIt is undefined.

Difference ebtween push_back and emplace_back is explained here.

Link : difference between emplace_back and push_back function