Seredenko-V

Insert for SimpleVector

Jul 18th, 2022 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1.     Iterator Insert(ConstIterator pos, const Type& value) {
  2.         if (capacity_ == 0) {
  3.             this->PushBack(value);
  4.         }
  5.         // Базовая гарантия безопасности исключений
  6.         if (size_ == capacity_) {
  7.             assert(size_ <= capacity_);
  8.             SimpleVector tmp_vector(size_ + 1, value); // чтобы записать в нужной позиции value
  9.             std::copy(this->cbegin(), pos, tmp_vector.begin());
  10.             std::copy_backward(pos, this->cend(), tmp_vector.end());
  11.             tmp_vector.capacity_ = size_ * 2;
  12.             size_t position_insert_value = std::distance(this->cbegin(), pos);
  13.             this->swap(tmp_vector);
  14.             //std::cout << "Insert(): " << simple_vector_[position_insert_value] << '\t' << value << std::endl;
  15.             return Iterator (&simple_vector_[position_insert_value]);
  16.         } else {
  17.             std::copy_backward(pos, this->cend(), std::next(this->end()));
  18.             std::copy(&value, &value + 1, const_cast<Iterator>(pos));
  19.             ++size_;
  20.             //std::cout << "Insert(): " << *pos << '\t' << value << std::endl;
  21.             return const_cast<Iterator>(pos);
  22.         }
  23.  
  24.         //// Стогая гарантия безопасности исключений
  25.         //SimpleVector tmp_vector(size_ + 1, value); // чтобы записать в нужной позиции value
  26.         //std::copy(this->cbegin(), pos, tmp_vector.begin());
  27.         //std::copy_backward(pos, this->cend(), tmp_vector.end());
  28.         //this->swap(tmp_vector);
  29.         //return const_cast<Iterator> (pos);
  30.     }
Add Comment
Please, Sign In to add comment