序列式容器-vector
来源:互联网 发布:java表达式 编辑:程序博客网 时间:2024/06/04 17:57
vector定义摘要: <vector>or更底层的<stl_vector.h>template<class T,class Alloc=alloc> //alloc是STL的空间配置器class vector{public://vector的嵌套型别定义typedef T value_type;typedef value_type* pointer;typedef value_type* iterator; //由此可见迭代器对象的型别即为普通指针型别typedef value_type& reference;typedef size_t size_type;typedef ptrdiff_t difference_type;protected://simple_alloc是SGI STL的空间配置器typedef simple_alloc<value_type,Alloc>data_allocator;iterator start;iterator finish;iterator end_of_storage; //备用范围被包括void insert_aux(iterator position, const T& x);void deallocate() { if(start) data_allocator::deallocate(start, end_of_storage-start);}void fill_initialize(size_type n,const T&value){start=allocate_and_fill(n,value);finish=start+n;end_of_storage=finish;}public:iterator begin() {return start;}iterator end() { return finish;}size_type size() const { return size_type(end()-begin()); }size_type capacity() const { return size_type(end_of_storage-begin()); }bool empty() const { return begin()==end();}reference operator[] (size_type n) {return *(begin()+n); }vector():start(0),finish(0), end_of_storage(0) {}vector(size_type n,const T&value) { fill_initialize(n,value); }vector(int n,const T&value) { fill_initialize(n,value); }vector(long n,const T&value) {fill_initialize(n,value); }explicit vector(size_type n) { fill_initialize(n,T()); }~vector() {destroy(start,finish); //全局函数deallocate();}reference front() {return *begin(); }reference back() {return *(end()-1); }void push_back(const T&x) { //将元素插至最尾端 if(finish!=end_of_storage) { construct(finish,x); //全局函数 ++finish; } else insert_aux(end(),x);}void pop_back() { //将最尾端元素取出 --finish; destroy(finish); //全局函数}iterator erase(iterator position) { //清除某位置上的元素 if(position+1!=end()) copy(position+1,finish,position); //后续元素往前移动 --finish; destroy(finish); return position; }void resize(size_type new_size, const T&x) {if(new_size<size()) erase(begin()+new_size,end());else insert(end(),new_size-size(),end()); }void resize(seiz_type new_size) { resize(new_size, T()); }void clear() { erase(begin(), end()); }protected://配置空间并填满内容iterator allocate_and_fill( size_type n, const T&x) {iterator result=data_allocator::allocate(n);uninitialized_fill_n(result, n, x); //全局函数return result;}
源于《STL源码剖析》
阅读全文
0 0
- 序列式容器----vector
- 序列式容器:vector
- 序列式容器 vector
- 序列式容器-vector
- 【STL】序列式容器:vector
- 【STL】序列式容器--vector
- STL容器-序列式容器vector
- 三 序列式容器(一)vector
- STL中序列式容器之一vector
- 【C++ STL】序列式容器Vector
- 序列式容器vector和deque
- vector——序列式容器
- C++ 序列式容器之vector
- STL:序列式容器vector总结
- C++序列式容器vector,deque,list
- STL序列式容器之vector
- Chapter 4: 序列式容器之 vector
- STL-序列式容器-vector详解
- unity升级新版本后ImagetTarget显示的是白色图片以及拓展识别Extended Tracking
- 485通信详解 (转载)
- Java LinkedHashMap 逆序遍历
- Sql Server 实现带字符的自增字段函数
- 使用 Go-Ethereum 1.7.2搭建以太坊私有链
- 序列式容器-vector
- 使用Spring发送Email
- 12·RHCSA创建归档
- Facenet
- JS编码规范
- 并发解决方案
- Date类型的时间显示格式
- Android使用代码为textview设置drawableLeft或drawableRight
- C和C++在结构体和类方面的不同