📄 vector.tcc
字号:
_M_insert_aux(iterator __position, const _Tp& __x) { if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) { this->_M_impl.construct(this->_M_impl._M_finish, *(this->_M_impl._M_finish - 1)); ++this->_M_impl._M_finish; _Tp __x_copy = __x; std::copy_backward(__position, iterator(this->_M_impl._M_finish-2), iterator(this->_M_impl._M_finish-1)); *__position = __x_copy; } else { const size_type __old_size = size(); if (__old_size == this->max_size()) __throw_length_error(__N("vector::_M_insert_aux")); // When sizeof(value_type) == 1 and __old_size > size_type(-1)/2 // __len overflows: if we don't notice and _M_allocate doesn't // throw we crash badly later. size_type __len = __old_size != 0 ? 2 * __old_size : 1; if (__len < __old_size) __len = this->max_size(); iterator __new_start(this->_M_allocate(__len)); iterator __new_finish(__new_start); try { __new_finish = std::__uninitialized_copy_a(iterator(this->_M_impl._M_start), __position, __new_start, _M_get_Tp_allocator()); this->_M_impl.construct(__new_finish.base(), __x); ++__new_finish; __new_finish = std::__uninitialized_copy_a(__position, iterator(this->_M_impl._M_finish), __new_finish, _M_get_Tp_allocator()); } catch(...) { std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator()); _M_deallocate(__new_start.base(),__len); __throw_exception_again; } std::_Destroy(begin(), end(), _M_get_Tp_allocator()); _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage - this->_M_impl._M_start); this->_M_impl._M_start = __new_start.base(); this->_M_impl._M_finish = __new_finish.base(); this->_M_impl._M_end_of_storage = __new_start.base() + __len; } } template<typename _Tp, typename _Alloc> void vector<_Tp, _Alloc>:: _M_fill_insert(iterator __position, size_type __n, const value_type& __x) { if (__n != 0) { if (size_type(this->_M_impl._M_end_of_storage - this->_M_impl._M_finish) >= __n) { value_type __x_copy = __x; const size_type __elems_after = end() - __position; iterator __old_finish(this->_M_impl._M_finish); if (__elems_after > __n) { std::__uninitialized_copy_a(this->_M_impl._M_finish - __n, this->_M_impl._M_finish, this->_M_impl._M_finish, _M_get_Tp_allocator()); this->_M_impl._M_finish += __n; std::copy_backward(__position, __old_finish - __n, __old_finish); std::fill(__position, __position + __n, __x_copy); } else { std::__uninitialized_fill_n_a(this->_M_impl._M_finish, __n - __elems_after, __x_copy, _M_get_Tp_allocator()); this->_M_impl._M_finish += __n - __elems_after; std::__uninitialized_copy_a(__position, __old_finish, this->_M_impl._M_finish, _M_get_Tp_allocator()); this->_M_impl._M_finish += __elems_after; std::fill(__position, __old_finish, __x_copy); } } else { const size_type __old_size = size(); if (this->max_size() - __old_size < __n) __throw_length_error(__N("vector::_M_fill_insert")); // See _M_insert_aux above. size_type __len = __old_size + std::max(__old_size, __n); if (__len < __old_size) __len = this->max_size(); iterator __new_start(this->_M_allocate(__len)); iterator __new_finish(__new_start); try { __new_finish = std::__uninitialized_copy_a(begin(), __position, __new_start, _M_get_Tp_allocator()); std::__uninitialized_fill_n_a(__new_finish, __n, __x, _M_get_Tp_allocator()); __new_finish += __n; __new_finish = std::__uninitialized_copy_a(__position, end(), __new_finish, _M_get_Tp_allocator()); } catch(...) { std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator()); _M_deallocate(__new_start.base(), __len); __throw_exception_again; } std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, _M_get_Tp_allocator()); _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage - this->_M_impl._M_start); this->_M_impl._M_start = __new_start.base(); this->_M_impl._M_finish = __new_finish.base(); this->_M_impl._M_end_of_storage = __new_start.base() + __len; } } } template<typename _Tp, typename _Alloc> template<typename _InputIterator> void vector<_Tp, _Alloc>:: _M_range_insert(iterator __pos, _InputIterator __first, _InputIterator __last, std::input_iterator_tag) { for (; __first != __last; ++__first) { __pos = insert(__pos, *__first); ++__pos; } } template<typename _Tp, typename _Alloc> template<typename _ForwardIterator> void vector<_Tp, _Alloc>:: _M_range_insert(iterator __position, _ForwardIterator __first, _ForwardIterator __last, std::forward_iterator_tag) { if (__first != __last) { const size_type __n = std::distance(__first, __last); if (size_type(this->_M_impl._M_end_of_storage - this->_M_impl._M_finish) >= __n) { const size_type __elems_after = end() - __position; iterator __old_finish(this->_M_impl._M_finish); if (__elems_after > __n) { std::__uninitialized_copy_a(this->_M_impl._M_finish - __n, this->_M_impl._M_finish, this->_M_impl._M_finish, _M_get_Tp_allocator()); this->_M_impl._M_finish += __n; std::copy_backward(__position, __old_finish - __n, __old_finish); std::copy(__first, __last, __position); } else { _ForwardIterator __mid = __first; std::advance(__mid, __elems_after); std::__uninitialized_copy_a(__mid, __last, this->_M_impl._M_finish, _M_get_Tp_allocator()); this->_M_impl._M_finish += __n - __elems_after; std::__uninitialized_copy_a(__position, __old_finish, this->_M_impl._M_finish, _M_get_Tp_allocator()); this->_M_impl._M_finish += __elems_after; std::copy(__first, __mid, __position); } } else { const size_type __old_size = size(); if (this->max_size() - __old_size < __n) __throw_length_error(__N("vector::_M_range_insert")); // See _M_insert_aux above. size_type __len = __old_size + std::max(__old_size, __n); if (__len < __old_size) __len = this->max_size(); iterator __new_start(this->_M_allocate(__len)); iterator __new_finish(__new_start); try { __new_finish = std::__uninitialized_copy_a(iterator(this->_M_impl._M_start), __position, __new_start, _M_get_Tp_allocator()); __new_finish = std::__uninitialized_copy_a(__first, __last, __new_finish, _M_get_Tp_allocator()); __new_finish = std::__uninitialized_copy_a(__position, iterator(this->_M_impl._M_finish), __new_finish, _M_get_Tp_allocator()); } catch(...) { std::_Destroy(__new_start,__new_finish, _M_get_Tp_allocator()); _M_deallocate(__new_start.base(), __len); __throw_exception_again; } std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, _M_get_Tp_allocator()); _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage - this->_M_impl._M_start); this->_M_impl._M_start = __new_start.base(); this->_M_impl._M_finish = __new_finish.base(); this->_M_impl._M_end_of_storage = __new_start.base() + __len; } } }} // namespace std#endif /* _VECTOR_TCC */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -