Seredenko-V

Insert with move

Jul 22nd, 2022
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1.    Iterator Insert(ConstIterator pos, Type value) {
  2.         if (capacity_ == 0) {
  3.             this->PushBack(std::move(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::move(cbegin(), pos, tmp_arr.Get());
  11.             *new_pos = std::move(value);
  12.             std::move_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.             std::move_backward(pos, this->cend(), std::next(this->end()));
  19.             std::move(&value, &value + 1, const_cast<Iterator>(pos));
  20.             ++size_;
  21.             return const_cast<Iterator>(pos);
  22.         }
  23.     }
Advertisement
Add Comment
Please, Sign In to add comment