I was asked this question in an interview.
The points I answered are like this
1) an index pointing to the current position;
2) resize if neccessary.
Can anybody elaborate more?
Vector Push_Back() Function in C++ The dynamic array can be implemented by using a vector in C++. The elements can be added to the vector in different ways. The push_back() function is one of the ways to insert a new element at the end of the vector that increases the size of the vector by 1.
push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the current last element and the container size is increased by 1.
You don't want to push_front on a vector ever. Adding an element to the front means moving every single other element in the vector one element back: O(n) copying.
The push_back method is used to append an element in a sequential STL container (e.g., std::vector). When inserted using the push_back, the new element is copy-or-move-constructed.
Like so:
void push_back(T const& param)
{
vector temp(rbegin(), rend());
temp.push_front(param);
*this = vector(temp.rbegin(), temp.rend());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With