Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Iterator Insert(ConstIterator pos, const Type& value) {
- if (capacity_ == 0) {
- this->PushBack(value);
- return this->begin();
- }
- // Базовая гарантия безопасности исключений
- if (size_ == capacity_) {
- assert(size_ <= capacity_);
- ArrayPtr<Type> tmp_arr(capacity_ * 2);
- Iterator new_pos = std::copy(cbegin(), pos, tmp_arr.Get());
- *new_pos = value;
- std::copy_backward(pos, cend(), tmp_arr.Get() + size_ + 1);
- ++size_;
- capacity_ *= 2;
- simple_vector_.swap(tmp_arr);
- return new_pos;
- } else {
- Iterator new_pos = std::copy_backward(pos, this->cend(), std::next(this->end()));
- *new_pos = value;
- ++size_;
- return new_pos;
- }
- }
- Iterator Insert(ConstIterator pos, Type&& value) {
- if (capacity_ == 0) {
- this->PushBack(std::move(value));
- return this->begin();
- }
- // Базовая гарантия безопасности исключений
- if (size_ == capacity_) {
- assert(size_ <= capacity_);
- ArrayPtr<Type> tmp_arr(capacity_ * 2);
- Iterator new_pos = this->begin() + (std::distance(this->cbegin(), pos)); // текущего вектора
- std::move(begin(), new_pos, tmp_arr.Get());
- // в позицию pos перемещаем значение value (во временный массив)
- *(tmp_arr.Get() + (std::distance(this->cbegin(), pos))) = std::move(value);
- std::move_backward(new_pos, end(), tmp_arr.Get() + size_ + 1);
- ++size_;
- capacity_ *= 2;
- simple_vector_ = std::move(tmp_arr);
- return new_pos;
- } else {
- Iterator new_pos = this->begin() + (std::distance(this->cbegin(), pos));
- std::move_backward(new_pos, this->end(), std::next(this->end()));
- *new_pos = std::move(value);
- ++size_;
- return new_pos;
- }
- }
- void Resize(size_t new_size) {
- if (new_size > size_) {
- SimpleVector new_vector(new_size);
- std::move(this->begin(), this->end(), new_vector.begin());
- this->simple_vector_ = std::move(new_vector.simple_vector_);
- if (new_size > capacity_) {
- this->capacity_ = std::max(this->capacity_ * 2, new_size);
- }
- }
- this->size_ = new_size;
- }
Advertisement
Add Comment
Please, Sign In to add comment