vector insert ok

This commit is contained in:
hugogogo
2022-06-10 16:47:56 +02:00
parent 68ada8ca68
commit c187d34ce7
4 changed files with 240 additions and 73 deletions

View File

@@ -259,9 +259,9 @@ VT_TPL typename VT::iterator VT::
}
it = end();
if (position != it)
{
{
_allocator.construct(it, *(it - 1));
while (--it != position)
while (it-- != position)
*(it + 1) = *it;
}
_allocator.destroy(position);
@@ -269,14 +269,87 @@ VT_TPL typename VT::iterator VT::
_size++;
return (position);
}
//VT_TPL void VT::
// insert(iterator position, size_type n, const value_type& val) {
//
//}
//VT_TPL template <class InputIterator> void VT::
// insert(iterator position, InputIterator first, InputIterator last) {
//
//}
VT_TPL void VT::
insert(iterator position, size_type n, const value_type& val) {
difference_type distance;
iterator it_end;
iterator it;
if (_size + n > _capacity)
{
distance = position - begin();
_increment_capacity(n);
position = begin() + distance;
}
it_end = end();
if (position != it_end)
{
it = it_end;
while (it + n != it_end && it != position)
{
it--;
_allocator.construct(it + n, *it);
}
while (it != position)
{
it--;
*(it + n) = *it;
}
_destroy(position, std::min(position + n, it_end));
}
for (size_type i = 0; i < n; i++, position++)
_allocator.construct(position, val);
_size += n;
}
VT_TPL template <class InputIterator>
typename enable_if< !is_integral<InputIterator>::value,void >::type VT::
insert(iterator position, InputIterator first, InputIterator last) {
difference_type dist;
difference_type n;
iterator it_end;
iterator it;
n = std::distance(first, last);
if (_size + n > _capacity)
{
dist = position - begin();
_increment_capacity(n);
position = begin() + dist;
}
it_end = end();
if (position != it_end)
{
it = it_end;
while (it + n != it_end && it != position)
{
it--;
_allocator.construct(it + n, *it);
}
while (it != position)
{
it--;
*(it + n) = *it;
}
_destroy(position, std::min(position + n, it_end));
}
// for (size_type i = 0; i < n; i++, position++)
// _allocator.construct(position, val);
while (first != last)
{
_allocator.construct(position, *first);
++position;
++first;
}
_size += n;
}
// erase -------------------------------------
VT_TPL typename VT::iterator VT::
erase(iterator position) {