Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ STL Vector: Push_back taking reference

Tags:

c++

stl

From the cpp documentation for std::vector, I see this:

void push_back ( const T& x );

I understand that push_back makes a copy of the object that I pass. But, why is the signature const T& ? By looking at this, I initially thought it takes a const reference of whatever object that I push to the vector.

like image 233
NPE Avatar asked Aug 01 '12 15:08

NPE


1 Answers

The object you push is passed by reference to avoid extra copy. Than a copy is placed in the vector.

like image 187
Andrew Avatar answered Oct 23 '22 17:10

Andrew