- 论坛徽章:
- 0
|
看了一下SGI的源码:
- template <class T, class Alloc>
- void vector<T, Alloc>::insert(iterator position, size_type n, const T& x) {
- if (n != 0) {
- if (size_type(end_of_storage - finish) >= n) {
- T x_copy = x;
- const size_type elems_after = finish - position;
- iterator old_finish = finish;
- if (elems_after > n) {
- uninitialized_copy(finish - n, finish, finish);
- finish += n;
- copy_backward(position, old_finish - n, old_finish);
- fill(position, position + n, x_copy);
- }
- else {
- uninitialized_fill_n(finish, n - elems_after, x_copy);
- finish += n - elems_after;
- uninitialized_copy(position, old_finish, finish);
- finish += elems_after;
- fill(position, old_finish, x_copy);
- }
- }
- else {
- const size_type old_size = size();
- const size_type len = old_size + max(old_size, n);
- iterator new_start = data_allocator::allocate(len);
- iterator new_finish = new_start;
- __STL_TRY {
- new_finish = uninitialized_copy(start, position, new_start);
- new_finish = uninitialized_fill_n(new_finish, n, x);
- new_finish = uninitialized_copy(position, finish, new_finish);
- }
- # ifdef __STL_USE_EXCEPTIONS
- catch(...) {
- destroy(new_start, new_finish);
- data_allocator::deallocate(new_start, len);
- throw;
- }
- # endif /* __STL_USE_EXCEPTIONS */
- destroy(start, finish);
- deallocate();
- start = new_start;
- finish = new_finish;
- end_of_storage = new_start + len;
- }
- }
- }
复制代码
仅以这三行来说明问题:
new_finish = uninitialized_copy(start, position, new_start);
new_finish = uninitialized_fill_n(new_finish, n, x);
new_finish = uninitialized_copy(position, finish, new_finish);
当start = position的时候,其实第一行代码是不起作用的,因此新插入的元素是插入在begin的前面了。 |
|