vector::emplace_back function


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

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

Parameters:
args – A single argument or list of arguments.

Return type
void

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

Code example

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

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

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

Output

23 56 89 981 1425

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

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

Link:vector::push_back function

Code example

class AA
{
string ss;

public:

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

string getString( ) const { return ss; }

~AA( ) { }
};

int main( )
{
vector<AA> 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<< elem.getString() << ” ” ;
}

cin.get( ) ;
return 0 ;
}

Output

String Gift New Happy Dome

In the first push_back we appended an AA object which is fine.In the emplace_back and the second push_back function call 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.If the arguments 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;

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

~BB( ) { }
};

vector<BB> 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

Link:Difference between emplace_back and push_back function.


 


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

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.

Code example

vector<int> vec;

vec.reserve( 3 ) ;

vector<int> ::iterator vecIt=vec.begin( ) ;

vec.emplace_back(90);
vec.emplace_back(120);
vec.emplace_back(910);

cout<< *vecIt << endl ;///iterator is still valid

vec.emplace_back( 4560 );

cout<< *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.Once the reallocation occurs the iterator vecIt becomes invalid.And hence accessing the storage using vecIt is undefined.