//_Alloc 表示内存分配器,此参数几乎不需要我们关心 template <class _Ty, class _Alloc = allocator<_Ty>> class vector{ ... protected: pointer _Myfirst; pointer _Mylast; pointer _Myend; };其中,_Myfirst 指向的是 vector 容器对象的起始字节位置;_Mylast 指向当前最后一个元素的末尾字节;_myend 指向整个 vector 容器所占用内存空间的末尾字节。
图 1 vector实现原理示意图
template <class _Ty, class _Alloc = allocator<_Ty>> class vector{ public: iterator begin() {return _Myfirst;} iterator end() {return _Mylast;} size_type size() const {return size_type(end() - begin());} size_type capacity() const {return size_type(_Myend - begin());} bool empty() const {return begin() == end();} reference operator[] (size_type n) {return *(begin() + n);} reference front() { return *begin();} reference back() {return *(end()-1);} ... };
本文链接:http://task.lmcjl.com/news/16262.html