D
digz
template<typename _Tp, typename _Alloc>
void
vector<_Tp, _Alloc>::
_M_insert_aux(iterator __position, const _Tp& __x)
{
if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
{
this->_M_impl.construct(this->_M_impl._M_finish,
*(this->_M_impl._M_finish - 1));
++this->_M_impl._M_finish;
_Tp __x_copy = __x;
std::copy_backward(__position,
iterator(this->_M_impl._M_finish-2),
iterator(this->_M_impl._M_finish-1));
*__position = __x_copy;
}
else ...
I had a couple of doubts with regard to code pasted here from
stl_vector.h
a) I was curious why a copy of x is created in the statement:
_Tp __x_copy = __x;
and that is assigned to *position, rather than directly doing
*position = __x ;
b) is it necessary to increment finish ++this->_M_impl._M_finish
before the copy_backward.
operation, is there anything wrong in doing the copy backwards first
like this
std::copy_backward(__position,
iterator(this->_M_impl._M_finish-1),
iterator(this->_M_impl._M_finish));
and then
++this->_M_impl._M_finish;
Thx
Digz
void
vector<_Tp, _Alloc>::
_M_insert_aux(iterator __position, const _Tp& __x)
{
if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
{
this->_M_impl.construct(this->_M_impl._M_finish,
*(this->_M_impl._M_finish - 1));
++this->_M_impl._M_finish;
_Tp __x_copy = __x;
std::copy_backward(__position,
iterator(this->_M_impl._M_finish-2),
iterator(this->_M_impl._M_finish-1));
*__position = __x_copy;
}
else ...
I had a couple of doubts with regard to code pasted here from
stl_vector.h
a) I was curious why a copy of x is created in the statement:
_Tp __x_copy = __x;
and that is assigned to *position, rather than directly doing
*position = __x ;
b) is it necessary to increment finish ++this->_M_impl._M_finish
before the copy_backward.
operation, is there anything wrong in doing the copy backwards first
like this
std::copy_backward(__position,
iterator(this->_M_impl._M_finish-1),
iterator(this->_M_impl._M_finish));
and then
++this->_M_impl._M_finish;
Thx
Digz