头文件 <stl_vector.h>
Vector封装了数组,并定义了二十个操作数组的方法,下面是stl_vector的基本架构
1 //////////////////////////////////////////////////////////////////////预处理/////////////////////////////////////////////////////////////////////////////////////// 2 3 #ifndef __SGI_STL_INTERNAL_VECTOR_H 4 #define __SGI_STL_INTERNAL_VECTOR_H 5 6 #include <concept_checks.h> 7 8 __STL_BEGIN_NAMESPACE 9 10 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) 11 #pragma set woff 1174 //禁止1174号编译器警告 12 #pragma set woff 1375 //禁止1375号编译器警告 13 #endif 14 15 //////////////////////////////////////////////////////////////////////预处理/////////////////////////////////////////////////////////////////////////////////////// 16 17 /////////////////////////////////////////////////////////////////////空间适配/////////////////////////////////////////////////////////////////////////////////////// 18 19 #ifdef __STL_USE_STD_ALLOCATORS 20 21 template <class _Tp, class _Allocator, bool _IsStatic> 22 class _Vector_alloc_base 23 { 24 public: 25 26 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type allocator_type; 27 allocator_type get_allocator() const { return _M_data_allocator; } 28 _Vector_alloc_base(const allocator_type& __a) : _M_data_allocator(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0) {} 29 30 protected: 31 32 allocator_type _M_data_allocator; 33 _Tp* _M_start; //指针头 34 _Tp* _M_finish; //指针尾 35 _Tp* _M_end_of_storage; //空间尾 36 37 _Tp* _M_allocate(size_t __n) { return _M_data_allocator.allocate(__n); } 38 void _M_deallocate(_Tp* __p, size_t __n) { if (__p) _M_data_allocator.deallocate(__p, __n); } 39 }; 40 41 template <class _Tp, class _Allocator> //不生成空间适配器对象 42 class _Vector_alloc_base<_Tp, _Allocator, true> 43 { 44 public: 45 46 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type allocator_type; 47 allocator_type get_allocator() const { return allocator_type(); } 48 _Vector_alloc_base(const allocator_type&) : _M_start(0), _M_finish(0), _M_end_of_storage(0) {} 49 50 protected: 51 52 _Tp* _M_start; 53 _Tp* _M_finish; 54 _Tp* _M_end_of_storage; 55 56 typedef typename _Alloc_traits<_Tp, _Allocator>::_Alloc_type _Alloc_type; 57 _Tp* _M_allocate(size_t __n) { return _Alloc_type::allocate(__n); } 58 void _M_deallocate(_Tp* __p, size_t __n) { _Alloc_type::deallocate(__p, __n);} 59 }; 60 61 template <class _Tp, class _Alloc> //继承自继承自vector基本空间适配器 62 struct _Vector_base : public _Vector_alloc_base<_Tp, _Alloc,_Alloc_traits<_Tp, _Alloc>::_S_instanceless> 63 { 64 typedef _Vector_alloc_base<_Tp, _Alloc,_Alloc_traits<_Tp, _Alloc>::_S_instanceless> _Base; 65 typedef typename _Base::allocator_type allocator_type; 66 67 _Vector_base(const allocator_type& __a) : _Base(__a) {} 68 _Vector_base(size_t __n, const allocator_type& __a) : _Base(__a) 69 { 70 _M_start = _M_allocate(__n); 71 _M_finish = _M_start; 72 _M_end_of_storage = _M_start + __n; 73 } 74 ~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); } 75 }; 76 77 #else /* __STL_USE_STD_ALLOCATORS */ 78 79 template <class _Tp, class _Alloc> 80 class _Vector_base 81 { 82 public: 83 84 typedef _Alloc allocator_type; 85 allocator_type get_allocator() const { return allocator_type(); } 86 87 _Vector_base(const _Alloc&) : _M_start(0), _M_finish(0), _M_end_of_storage(0) {} 88 _Vector_base(size_t __n, const _Alloc&) : _M_start(0), _M_finish(0), _M_end_of_storage(0) 89 { 90 _M_start = _M_allocate(__n); 91 _M_finish = _M_start; 92 _M_end_of_storage = _M_start + __n; 93 } 94 95 ~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); } 96 97 protected: 98 99 _Tp* _M_start; 100 _Tp* _M_finish; 101 _Tp* _M_end_of_storage; 102 103 typedef simple_alloc<_Tp, _Alloc> _M_data_allocator; 104 _Tp* _M_allocate(size_t __n) { return _M_data_allocator::allocate(__n); } 105 void _M_deallocate(_Tp* __p, size_t __n) { _M_data_allocator::deallocate(__p, __n); } 106 }; 107 108 #endif /* __STL_USE_STD_ALLOCATORS */ 109 110 /////////////////////////////////////////////////////////////////////空间适配/////////////////////////////////////////////////////////////////////////////////////// 111 112 /////////////////////////////////////////////////////////////////////vector定义//////////////////////////////////////////////////////////////////////////////////// 113 114 template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) > 115 class vector : protected _Vector_base<_Tp, _Alloc> 116 { 117 __STL_CLASS_REQUIRES(_Tp, _Assignable); 118 119 private: 120 121 typedef _Vector_base<_Tp, _Alloc> _Base; 122 123 public: 124 125 typedef _Tp value_type; //数据类型 126 typedef value_type* pointer; //数据类型指针 127 typedef const value_type* const_pointer; //静态数据类型指针 128 typedef value_type* iterator; //数据类型迭代器 129 typedef const value_type* const_iterator; //静态数据类型迭代器 130 typedef value_type& reference; //数据类型引用 131 typedef const value_type& const_reference; //静态数据类型引用 132 typedef size_t size_type; //内存申请单位,unsigned类型 133 typedef ptrdiff_t difference_type; //两个指针相减的结果,long int类型 134 135 typedef typename _Base::allocator_type allocator_type; 136 allocator_type get_allocator() const { return _Base::get_allocator(); } 137 138 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION //反向迭代器定义 139 typedef reverse_iterator<const_iterator> const_reverse_iterator; 140 typedef reverse_iterator<iterator> reverse_iterator; 141 #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */ 142 typedef reverse_iterator<const_iterator, value_type, const_reference, difference_type> const_reverse_iterator; 143 typedef reverse_iterator<iterator, value_type, reference, difference_type> reverse_iterator; 144 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */ 145 146 protected: 147 148 //名称引入 149 #ifdef __STL_HAS_NAMESPACES 150 using _Base::_M_allocate; 151 using _Base::_M_deallocate; 152 using _Base::_M_start; 153 using _Base::_M_finish; 154 using _Base::_M_end_of_storage; 155 #endif /* __STL_HAS_NAMESPACES */ 156 157 //向前声明 158 void _M_insert_aux(iterator __position, const _Tp& __x); 159 void _M_insert_aux(iterator __position); 160 void _M_fill_assign(size_type __n, const _Tp& __val); 161 vector<_Tp, _Alloc>& operator=(const vector<_Tp, _Alloc>& __x); 162 void _M_fill_insert (iterator __pos, size_type __n, const _Tp& __x); 163 template <class _InputIterator> 164 void _M_assign_aux(_InputIterator __first, _InputIterator __last,input_iterator_tag); 165 template <class _ForwardIterator> 166 void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,forward_iterator_tag); 167 168 /////////////////////////////////////////////////////////////////////构造函数/////////////////////////////////////////////////////////////////////////////////////// 169 170 public: 171 172 explicit vector(const allocator_type& __a = allocator_type()) : _Base(__a) {} //简单的构造 173 174 vector(size_type __n, const _Tp& __value,const allocator_type& __a = allocator_type()) : _Base(__n, __a) 175 { _M_finish = uninitialized_fill_n(_M_start, __n, __value); } //用__value值构造包含__n个元素的Vector 176 177 explicit vector(size_type __n) : _Base(__n, allocator_type()) 178 { _M_finish = uninitialized_fill_n(_M_start, __n, _Tp()); } //用默认值构造包含__n个元素的Vector 179 180 vector(const vector<_Tp, _Alloc>& __x) : _Base(__x.size(), __x.get_allocator()) //拷贝构造函数 181 { _M_finish = uninitialized_copy(__x.begin(), __x.end(), _M_start); } 182 183 //特化Vector类,接受两个指针或者迭代器,完成对象创建 184 #ifdef __STL_MEMBER_TEMPLATES 185 186 template <class _InputIterator> 187 vector(_InputIterator __first, _InputIterator __last,const allocator_type& __a = allocator_type()) : _Base(__a) 188 { 189 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 190 _M_initialize_aux(__first, __last, _Integral()); 191 } 192 193 template <class _Integer> 194 void _M_initialize_aux(_Integer __n, _Integer __value, __true_type) 195 { 196 _M_start = _M_allocate(__n); 197 _M_end_of_storage = _M_start + __n; 198 _M_finish = uninitialized_fill_n(_M_start, __n, __value); 199 } 200 201 template <class _InputIterator> 202 void _M_initialize_aux(_InputIterator __first, _InputIterator __last,__false_type) 203 { 204 _M_range_initialize(__first, __last, __ITERATOR_CATEGORY(__first)); 205 } 206 207 #else 208 vector(const _Tp* __first, const _Tp* __last,const allocator_type& __a = allocator_type()) : _Base(__last - __first, __a) 209 { _M_finish = uninitialized_copy(__first, __last, _M_start); } 210 #endif /* __STL_MEMBER_TEMPLATES */ 211 212 ~vector() { destroy(_M_start, _M_finish); } 213 214 /////////////////////////////////////////////////////////////////////构造函数/////////////////////////////////////////////////////////////////////////////////////// 215 216 //////////////////////////////////////////////////////////////////Vector方法定义///////////////////////////////////////////////////////////////////////////////////* 217 218 iterator begin() { return _M_start; } //返回起始地址 219 const_iterator begin() const { return _M_start; } //返回静态起始地址 220 iterator end() { return _M_finish; } //返回结束地址 221 const_iterator end() const { return _M_finish; } //返回静态结束地址 222 223 reverse_iterator rbegin() { return reverse_iterator(end()); } //返回结束地址 224 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } //返回静态结束地址 225 reverse_iterator rend() { return reverse_iterator(begin()); } //返回起始地址 226 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } //返回静态起始地址 227 228 reference front() { return *begin(); } //返回头元素 229 const_reference front() const { return *begin(); } 230 reference back() { return *(end() - 1); } //返回尾元素 231 const_reference back() const { return *(end() - 1); } 232 233 size_type size() const { return size_type(end() - begin()); } //数组空间当前大小 234 size_type max_size() const { return size_type(-1) / sizeof(_Tp); } //允许最多的元素个数 235 size_type capacity() const { return size_type(_M_end_of_storage - begin()); } //数组空间总大小 236 bool empty() const { return begin() == end(); } //数组是否空 237 238 void clear() { erase(begin(), end()); } //清空数组 239 240 void pop_back() //删除尾部元素 241 { 242 --_M_finish; 243 destroy(_M_finish); 244 } 245 246 iterator erase(iterator __position) //删除指定位置元素 247 { 248 if (__position + 1 != end()) 249 copy(__position + 1, _M_finish, __position); 250 --_M_finish; 251 destroy(_M_finish); 252 return __position; 253 } 254 iterator erase(iterator __first, iterator __last) 255 { 256 iterator __i = copy(__last, _M_finish, __first); 257 destroy(__i, _M_finish); 258 _M_finish = _M_finish - (__last - __first); 259 return __first; 260 } 261 262 #ifdef __STL_THROW_RANGE_ERRORS 263 void _M_range_check(size_type __n) const //数组空间溢出检测 264 { 265 if (__n >= this->size()) __stl_throw_range_error("vector"); 266 } 267 reference at(size_type __n) { _M_range_check(__n); return (*this)[__n]; } //at()函数,比[]符号更安全的访问元素函数 268 const_reference at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; } 269 #endif /* __STL_THROW_RANGE_ERRORS */ 270 271 void resize(size_type __new_size, const _Tp& __x) //重新调整数组空间大小 272 { 273 if (__new_size < size()) 274 erase(begin() + __new_size, end()); 275 else 276 insert(end(), __new_size - size(), __x); 277 } 278 void resize(size_type __new_size) { resize(__new_size, _Tp()); } 279 280 void reserve(size_type __n) //调整数组空间大小至少为__n 281 { 282 if (capacity() < __n) 283 { 284 const size_type __old_size = size(); 285 iterator __tmp = _M_allocate_and_copy(__n, _M_start, _M_finish); 286 destroy(_M_start, _M_finish); 287 _M_deallocate(_M_start, _M_end_of_storage - _M_start); 288 _M_start = __tmp; 289 _M_finish = __tmp + __old_size; 290 _M_end_of_storage = _M_start + __n; 291 } 292 } 293 294 void push_back(const _Tp& __x) //尾部追加元素 295 { 296 if (_M_finish != _M_end_of_storage) 297 { 298 construct(_M_finish, __x); 299 ++_M_finish; 300 } 301 else 302 _M_insert_aux(end(), __x); 303 } 304 void push_back() 305 { 306 if (_M_finish != _M_end_of_storage) 307 { 308 construct(_M_finish); 309 ++_M_finish; 310 } 311 else 312 _M_insert_aux(end()); 313 } 314 315 void swap(vector<_Tp, _Alloc>& __x) //数组指针交换 316 { 317 __STD::swap(_M_start, __x._M_start); 318 __STD::swap(_M_finish, __x._M_finish); 319 __STD::swap(_M_end_of_storage, __x._M_end_of_storage); 320 } 321 template <class _Tp, class _Alloc> 322 inline void swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y) 323 { 324 __x.swap(__y); 325 } 326 327 iterator insert(iterator __position, const _Tp& __x) //在指定位置插入数据 328 { 329 size_type __n = __position - begin(); 330 if (_M_finish != _M_end_of_storage && __position == end()) 331 { 332 construct(_M_finish, __x); 333 ++_M_finish; 334 } 335 else 336 _M_insert_aux(__position, __x); 337 return begin() + __n; 338 } 339 iterator insert(iterator __position) 340 { 341 size_type __n = __position - begin(); 342 if (_M_finish != _M_end_of_storage && __position == end()) 343 { 344 construct(_M_finish); 345 ++_M_finish; 346 } 347 else 348 _M_insert_aux(__position); 349 return begin() + __n; 350 } 351 352 #ifdef __STL_MEMBER_TEMPLATES 353 template <class _InputIterator> 354 void insert(iterator __pos, _InputIterator __first, _InputIterator __last) 355 { 356 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 357 _M_insert_dispatch(__pos, __first, __last, _Integral()); 358 } 359 360 template <class _Integer> 361 void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,__true_type) 362 { _M_fill_insert(__pos, (size_type) __n, (_Tp) __val); } 363 364 template <class _InputIterator> 365 void _M_insert_dispatch(iterator __pos,_InputIterator __first, _InputIterator __last,__false_type) 366 { _M_range_insert(__pos, __first, __last, __ITERATOR_CATEGORY(__first)); } 367 #else /* __STL_MEMBER_TEMPLATES */ 368 void insert(iterator __position,const_iterator __first, const_iterator __last); 369 #endif /* __STL_MEMBER_TEMPLATES */ 370 void insert (iterator __pos, size_type __n, const _Tp& __x) { _M_fill_insert(__pos, __n, __x); } 371 372 //在Vector中填充值 373 void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); } 374 #ifdef __STL_MEMBER_TEMPLATES 375 template <class _InputIterator> 376 void assign(_InputIterator __first, _InputIterator __last) 377 { 378 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 379 _M_assign_dispatch(__first, __last, _Integral()); 380 } 381 382 template <class _Integer> 383 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 384 { _M_fill_assign((size_type) __n, (_Tp) __val); } 385 386 template <class _InputIter> 387 void _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type) 388 { _M_assign_aux(__first, __last, __ITERATOR_CATEGORY(__first)); } 389 #endif /* __STL_MEMBER_TEMPLATES */ 390 391 //////////////////////////////////////////////////////////////////Vector方法定义///////////////////////////////////////////////////////////////////////////////////* 392 393 ////////////////////////////////////////////////////////////////////操作符重载//////////////////////////////////////////////////////////////////////////////////// 394 395 protected: 396 397 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER 398 399 //[]号重载 400 reference operator[](size_type __n) { return *(begin() + __n); } 401 const_reference operator[](size_type __n) const { return *(begin() + __n); } 402 403 //==号重载 404 template <class _Tp, class _Alloc> 405 inline bool operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 406 { 407 return __x.size() == __y.size() && equal(__x.begin(), __x.end(), __y.begin()); 408 } 409 410 ////!=号重载 411 template <class _Tp, class _Alloc> 412 inline bool operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return !(__x == __y); } 413 414 //<号重载 415 template <class _Tp, class _Alloc> 416 inline bool operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 417 { 418 return lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end()); 419 } 420 421 //>号重载 422 template <class _Tp, class _Alloc> 423 inline bool operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return __y < __x; } 424 425 //<=号重载 426 template <class _Tp, class _Alloc> 427 inline bool operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return !(__y < __x); } 428 429 //>=号重载 430 template <class _Tp, class _Alloc> 431 inline bool operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) { return !(__x < __y); } 432 433 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */ 434 435 //赋值操作符重载 436 template <class _Tp, class _Alloc> 437 vector<_Tp,_Alloc>& vector<_Tp,_Alloc>::operator=(const vector<_Tp, _Alloc>& __x) 438 { 439 if (&__x != this) 440 { 441 const size_type __xlen = __x.size(); 442 if (__xlen > capacity()) 443 { 444 iterator __tmp = _M_allocate_and_copy(__xlen, __x.begin(), __x.end()); 445 destroy(_M_start, _M_finish); 446 _M_deallocate(_M_start, _M_end_of_storage - _M_start); 447 _M_start = __tmp; 448 _M_end_of_storage = _M_start + __xlen; 449 } 450 else if (size() >= __xlen) { 451 iterator __i = copy(__x.begin(), __x.end(), begin()); 452 destroy(__i, _M_finish); 453 } 454 else 455 { 456 copy(__x.begin(), __x.begin() + size(), _M_start); 457 uninitialized_copy(__x.begin() + size(), __x.end(), _M_finish); 458 } 459 _M_finish = _M_start + __xlen; 460 } 461 return *this; 462 } 463 464 ////////////////////////////////////////////////////////////////////操作符重载//////////////////////////////////////////////////////////////////////////////////// 465 466 ///////////////////////////////////////////////////////////////////辅助函数定义////////////////////////////////////////////////////////////////////////////////////// 467 468 //定义_M_allocate_and_copy,申请__n大小的空间,将__firsr到__last中的元素拷贝至新申请的空间中,返回新申请空间的首地址 469 #ifdef __STL_MEMBER_TEMPLATES 470 template <class _ForwardIterator> 471 iterator _M_allocate_and_copy(size_type __n, _ForwardIterator __first, _ForwardIterator __last) 472 { 473 iterator __result = _M_allocate(__n); 474 __STL_TRY 475 { 476 uninitialized_copy(__first, __last, __result); 477 return __result; 478 } 479 __STL_UNWIND(_M_deallocate(__result, __n)); 480 } 481 #else /* __STL_MEMBER_TEMPLATES */ 482 iterator _M_allocate_and_copy(size_type __n, const_iterator __first, const_iterator __last) 483 { 484 iterator __result = _M_allocate(__n); 485 __STL_TRY 486 { 487 uninitialized_copy(__first, __last, __result); 488 return __result; 489 } 490 __STL_UNWIND(_M_deallocate(__result, __n)); 491 } 492 #endif /* __STL_MEMBER_TEMPLATES */ 493 494 //定义_M_range_initialize,将__firsr到__last中的元素拷贝至已将存在的空见或新申请的空间中址 495 #ifdef __STL_MEMBER_TEMPLATES 496 template <class _InputIterator> 497 void _M_range_initialize(_InputIterator __first, _InputIterator __last, input_iterator_tag) 498 { 499 for ( ; __first != __last; ++__first) 500 push_back(*__first); 501 } 502 503 template <class _ForwardIterator> 504 void _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last, forward_iterator_tag) 505 { 506 size_type __n = 0; 507 distance(__first, __last, __n); 508 _M_start = _M_allocate(__n); 509 _M_end_of_storage = _M_start + __n; 510 _M_finish = uninitialized_copy(__first, __last, _M_start); 511 } 512 513 template <class _InputIterator> 514 void _M_range_insert(iterator __pos,_InputIterator __first, _InputIterator __last,input_iterator_tag); 515 516 template <class _ForwardIterator> 517 void _M_range_insert(iterator __pos,_ForwardIterator __first, _ForwardIterator __last,forward_iterator_tag); 518 519 #endif /* __STL_MEMBER_TEMPLATES */ 520 }; 521 522 //定义_M_fill_assign,为Vector中大小为__n的区域内填充值__val 523 template <class _Tp, class _Alloc> 524 void vector<_Tp, _Alloc>::_M_fill_assign(size_t __n, const value_type& __val) 525 { 526 if (__n > capacity()) 527 { 528 vector<_Tp, _Alloc> __tmp(__n, __val, get_allocator()); 529 __tmp.swap(*this); 530 } 531 else if (__n > size()) 532 { 533 fill(begin(), end(), __val); 534 _M_finish = uninitialized_fill_n(_M_finish, __n - size(), __val); 535 } 536 else 537 erase(fill_n(begin(), __n, __val), end()); 538 } 539 540 #ifdef __STL_MEMBER_TEMPLATES 541 542 //定义_M_assign_aux,将__first到__last中的元素逐个赋值到当前Vector中 543 template <class _Tp, class _Alloc> 544 template <class _InputIter> 545 void vector<_Tp, _Alloc>::_M_assign_aux(_InputIter __first, _InputIter __last,input_iterator_tag) 546 { 547 iterator __cur = begin(); 548 for ( ; __first != __last && __cur != end(); ++__cur, ++__first) 549 *__cur = *__first; 550 if (__first == __last) //若被复制的Vector空间小于复制Vector的空间,则清除被复制的Vector多余的空间 551 erase(__cur, end()); 552 else //若被复制的Vector空间大于复制Vector的空间,则将多余的元素插入到被复制的Vector后 553 insert(end(), __first, __last); 554 } 555 556 template <class _Tp, class _Alloc> 557 template <class _ForwardIter> 558 void vector<_Tp, _Alloc>::_M_assign_aux(_ForwardIter __first, _ForwardIter __last,forward_iterator_tag) 559 { 560 size_type __len = 0; 561 distance(__first, __last, __len); 562 563 if (__len > capacity()) 564 { 565 iterator __tmp = _M_allocate_and_copy(__len, __first, __last); 566 destroy(_M_start, _M_finish); 567 _M_deallocate(_M_start, _M_end_of_storage - _M_start); 568 _M_start = __tmp; 569 _M_end_of_storage = _M_finish = _M_start + __len; 570 } 571 else if (size() >= __len) 572 { 573 iterator __new_finish = copy(__first, __last, _M_start); 574 destroy(__new_finish, _M_finish); 575 _M_finish = __new_finish; 576 } 577 else 578 { 579 _ForwardIter __mid = __first; 580 advance(__mid, size()); 581 copy(__first, __mid, _M_start); 582 _M_finish = uninitialized_copy(__mid, __last, _M_finish); 583 } 584 } 585 586 #endif /* __STL_MEMBER_TEMPLATES */ 587 588 //定义_M_insert_aux,在Vector中的__position处插入值__x 589 template <class _Tp, class _Alloc> 590 void vector<_Tp, _Alloc>::_M_insert_aux(iterator __position, const _Tp& __x) 591 { 592 if (_M_finish != _M_end_of_storage) 593 { 594 construct(_M_finish, *(_M_finish - 1)); 595 ++_M_finish; 596 _Tp __x_copy = __x; 597 copy_backward(__position, _M_finish - 2, _M_finish - 1); 598 *__position = __x_copy; 599 } 600 else { 601 const size_type __old_size = size(); 602 const size_type __len = __old_size != 0 ? 2 * __old_size : 1; 603 iterator __new_start = _M_allocate(__len); 604 iterator __new_finish = __new_start; 605 __STL_TRY { 606 __new_finish = uninitialized_copy(_M_start, __position, __new_start); 607 construct(__new_finish, __x); 608 ++__new_finish; 609 __new_finish = uninitialized_copy(__position, _M_finish, __new_finish); 610 } 611 __STL_UNWIND((destroy(__new_start,__new_finish), _M_deallocate(__new_start,__len))); 612 destroy(begin(), end()); 613 _M_deallocate(_M_start, _M_end_of_storage - _M_start); 614 _M_start = __new_start; 615 _M_finish = __new_finish; 616 _M_end_of_storage = __new_start + __len; 617 } 618 } 619 620 template <class _Tp, class _Alloc> //在Vector中的__position处插入值一个默认值 621 void vector<_Tp, _Alloc>::_M_insert_aux(iterator __position) 622 { 623 if (_M_finish != _M_end_of_storage) 624 { 625 construct(_M_finish, *(_M_finish - 1)); 626 ++_M_finish; 627 copy_backward(__position, _M_finish - 2, _M_finish - 1); 628 *__position = _Tp(); 629 } 630 else 631 { 632 const size_type __old_size = size(); 633 const size_type __len = __old_size != 0 ? 2 * __old_size : 1; 634 iterator __new_start = _M_allocate(__len); 635 iterator __new_finish = __new_start; 636 __STL_TRY { 637 __new_finish = uninitialized_copy(_M_start, __position, __new_start); 638 construct(__new_finish); 639 ++__new_finish; 640 __new_finish = uninitialized_copy(__position, _M_finish, __new_finish); 641 } 642 __STL_UNWIND((destroy(__new_start,__new_finish), _M_deallocate(__new_start,__len))); 643 destroy(begin(), end()); 644 _M_deallocate(_M_start, _M_end_of_storage - _M_start); 645 _M_start = __new_start; 646 _M_finish = __new_finish; 647 _M_end_of_storage = __new_start + __len; 648 } 649 } 650 651 //定义_M_fill_insert,在Vector的__position位置后插插入连续大小为__n的值__x 652 template <class _Tp, class _Alloc> 653 void vector<_Tp, _Alloc>::_M_fill_insert(iterator __position, size_type __n, const _Tp& __x) 654 { 655 if (__n != 0) 656 { 657 if (size_type(_M_end_of_storage - _M_finish) >= __n) 658 { 659 _Tp __x_copy = __x; 660 const size_type __elems_after = _M_finish - __position; 661 iterator __old_finish = _M_finish; 662 if (__elems_after > __n) 663 { 664 uninitialized_copy(_M_finish - __n, _M_finish, _M_finish); 665 _M_finish += __n; 666 copy_backward(__position, __old_finish - __n, __old_finish); 667 fill(__position, __position + __n, __x_copy); 668 } 669 else { 670 uninitialized_fill_n(_M_finish, __n - __elems_after, __x_copy); 671 _M_finish += __n - __elems_after; 672 uninitialized_copy(__position, __old_finish, _M_finish); 673 _M_finish += __elems_after; 674 fill(__position, __old_finish, __x_copy); 675 } 676 } 677 else { 678 const size_type __old_size = size(); 679 const size_type __len = __old_size + max(__old_size, __n); 680 iterator __new_start = _M_allocate(__len); 681 iterator __new_finish = __new_start; 682 __STL_TRY 683 { 684 __new_finish = uninitialized_copy(_M_start, __position, __new_start); 685 __new_finish = uninitialized_fill_n(__new_finish, __n, __x); 686 __new_finish = uninitialized_copy(__position, _M_finish, __new_finish); 687 } 688 __STL_UNWIND((destroy(__new_start,__new_finish),_M_deallocate(__new_start,__len))); 689 destroy(_M_start, _M_finish); 690 _M_deallocate(_M_start, _M_end_of_storage - _M_start); 691 _M_start = __new_start; 692 _M_finish = __new_finish; 693 _M_end_of_storage = __new_start + __len; 694 } 695 } 696 } 697 698 #ifdef __STL_MEMBER_TEMPLATES 699 700 //定义_M_range_insert,在Vector的__position位置后插入__first到__last之间的值 701 template <class _Tp, class _Alloc> 702 template <class _InputIterator> 703 void vector<_Tp, _Alloc>::_M_range_insert(iterator __pos, _InputIterator __first, _InputIterator __last, input_iterator_tag) 704 { 705 for ( ; __first != __last; ++__first) 706 { 707 __pos = insert(__pos, *__first); 708 ++__pos; 709 } 710 } 711 712 template <class _Tp, class _Alloc> 713 template <class _ForwardIterator> 714 void vector<_Tp, _Alloc>::_M_range_insert(iterator __position, _ForwardIterator __first, _ForwardIterator __last, forward_iterator_tag) 715 { 716 if (__first != __last) 717 { 718 size_type __n = 0; 719 distance(__first, __last, __n); 720 if (size_type(_M_end_of_storage - _M_finish) >= __n) 721 { 722 const size_type __elems_after = _M_finish - __position; 723 iterator __old_finish = _M_finish; 724 if (__elems_after > __n) 725 { 726 uninitialized_copy(_M_finish - __n, _M_finish, _M_finish); 727 _M_finish += __n; 728 copy_backward(__position, __old_finish - __n, __old_finish); 729 copy(__first, __last, __position); 730 } 731 else { 732 _ForwardIterator __mid = __first; 733 advance(__mid, __elems_after); 734 uninitialized_copy(__mid, __last, _M_finish); 735 _M_finish += __n - __elems_after; 736 uninitialized_copy(__position, __old_finish, _M_finish); 737 _M_finish += __elems_after; 738 copy(__first, __mid, __position); 739 } 740 } 741 else { 742 const size_type __old_size = size(); 743 const size_type __len = __old_size + max(__old_size, __n); 744 iterator __new_start = _M_allocate(__len); 745 iterator __new_finish = __new_start; 746 __STL_TRY 747 { 748 __new_finish = uninitialized_copy(_M_start, __position, __new_start); 749 __new_finish = uninitialized_copy(__first, __last, __new_finish); 750 __new_finish = uninitialized_copy(__position, _M_finish, __new_finish); 751 } 752 __STL_UNWIND((destroy(__new_start,__new_finish), _M_deallocate(__new_start,__len))); 753 destroy(_M_start, _M_finish); 754 _M_deallocate(_M_start, _M_end_of_storage - _M_start); 755 _M_start = __new_start; 756 _M_finish = __new_finish; 757 _M_end_of_storage = __new_start + __len; 758 } 759 } 760 } 761 762 #else /* __STL_MEMBER_TEMPLATES */ 763 764 //定义insert函数,在__position后插入__first到__last之间的数据 765 template <class _Tp, class _Alloc> 766 void vector<_Tp, _Alloc>::insert(iterator __position, const_iterator __first, const_iterator __last) 767 { 768 if (__first != __last) 769 { 770 size_type __n = 0; 771 distance(__first, __last, __n); 772 if (size_type(_M_end_of_storage - _M_finish) >= __n) 773 { 774 const size_type __elems_after = _M_finish - __position; 775 iterator __old_finish = _M_finish; 776 if (__elems_after > __n) 777 { 778 uninitialized_copy(_M_finish - __n, _M_finish, _M_finish); 779 _M_finish += __n; 780 copy_backward(__position, __old_finish - __n, __old_finish); 781 copy(__first, __last, __position); 782 } 783 else 784 { 785 uninitialized_copy(__first + __elems_after, __last, _M_finish); 786 _M_finish += __n - __elems_after; 787 uninitialized_copy(__position, __old_finish, _M_finish); 788 _M_finish += __elems_after; 789 copy(__first, __first + __elems_after, __position); 790 } 791 } 792 else { 793 const size_type __old_size = size(); 794 const size_type __len = __old_size + max(__old_size, __n); 795 iterator __new_start = _M_allocate(__len); 796 iterator __new_finish = __new_start; 797 __STL_TRY { 798 __new_finish = uninitialized_copy(_M_start, __position, __new_start); 799 __new_finish = uninitialized_copy(__first, __last, __new_finish); 800 __new_finish = uninitialized_copy(__position, _M_finish, __new_finish); 801 } 802 __STL_UNWIND((destroy(__new_start,__new_finish),_M_deallocate(__new_start,__len))); 803 destroy(_M_start, _M_finish); 804 _M_deallocate(_M_start, _M_end_of_storage - _M_start); 805 _M_start = __new_start; 806 _M_finish = __new_finish; 807 _M_end_of_storage = __new_start + __len; 808 } 809 } 810 } 811 812 ///////////////////////////////////////////////////////////////////辅助函数定义////////////////////////////////////////////////////////////////////////////////////// 813 814 //////////////////////////////////////////////////////////////////////预处理////////////////////////////////////////////////////////////////////////////////////// 815 816 #endif /* __STL_MEMBER_TEMPLATES */ 817 818 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) 819 #pragma reset woff 1174 820 #pragma reset woff 1375 821 #endif 822 823 __STL_END_NAMESPACE 824 825 #endif /* __SGI_STL_INTERNAL_VECTOR_H */ 826 //////////////////////////////////////////////////////////////////////预处理//////////////////////////////////////////////////////////////////////////////////////