Seredenko-V

Untitled

Jul 24th, 2022
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. Iterator Insert(ConstIterator pos, const Type& value) {
  2.         if (capacity_ == 0) {
  3.             this->PushBack(value);
  4.             return this->begin();
  5.         }
  6.         // Базовая гарантия безопасности исключений
  7.         if (size_ == capacity_) {
  8.             assert(size_ <= capacity_);
  9.             ArrayPtr<Type> tmp_arr(capacity_ * 2);
  10.             Iterator new_pos = std::copy(cbegin(), pos, tmp_arr.Get());
  11.             *new_pos = value;
  12.             std::copy_backward(pos, cend(), tmp_arr.Get() + size_ + 1);
  13.             ++size_;
  14.             capacity_ *= 2;
  15.             simple_vector_.swap(tmp_arr);
  16.             return new_pos;
  17.         } else {
  18.             //Iterator new_pos = std::copy_backward(pos, this->cend(), std::next(this->end()));
  19.             //*new_pos = value;
  20.             std::copy_backward(pos, this->cend(), std::next(this->end()));
  21.             std::copy(&value, &value + 1, const_cast<Iterator>(pos));
  22.             ++size_;
  23.             return const_cast<Iterator>(pos);
  24.         }
  25.     }
  26.  
  27.     Iterator Insert(Iterator pos, Type&& value) {
  28.         if (capacity_ == 0) {
  29.             this->PushBack(std::move(value));
  30.             return this->begin();
  31.         }
  32.         // Базовая гарантия безопасности исключений
  33.         if (size_ == capacity_) {
  34.             assert(size_ <= capacity_);
  35.             ArrayPtr<Type> tmp_arr(capacity_ * 2);
  36.             Iterator new_pos = std::move(begin(), pos, tmp_arr.Get());
  37.             *new_pos = std::move(value);
  38.             std::move_backward(new_pos, end(), tmp_arr.Get() + size_ + 1);
  39.             ++size_;
  40.             capacity_ *= 2;
  41.             simple_vector_ = std::move(tmp_arr);
  42.             return new_pos;
  43.         } else {
  44.             Iterator new_pos = std::move_backward(pos, this->end(), std::next(this->end()));
  45.             *new_pos = std::move(value);
  46.             ++size_;
  47.             return new_pos;
  48.         }
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment