vector实现

来源:互联网 发布:软件开发单价 编辑:程序博客网 时间:2024/06/04 11:12

vector实现:

程序在windows xp下vs2010中编译通过。

//vector.h#ifndef VECTOR_H#define VECTOR_H#include <iostream>#include <algorithm>#include "construct.h"using std::allocator;template <typename T, typename Alloc = allocator<T> >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 differenct_type;protected://SGI STL中包装了一层simple_alloc<>/*typedef simple_alloc< value_type, Alloc > data_allocator;*/Alloc alloc;iterator start;iterator finish;iterator end_of_storage;void deallocate() {if(start) {alloc.deallocate(start, end_of_storage - start);}}void insert_aux(iterator, const T&);void fill_initialize(size_type n, const T &value) {start = allocate_and_fill(n, value);finish = start + n;end_of_storage = finish;}iterator allocate_and_fill(size_type n, const T &x) {iterator result = alloc.allocate(n);uninitialized_fill_n(result, n, x);return result;}public:iterator begin() {return start;}iterator end() {return finish;}size_type size() {return size_type(end() - begin());}size_type capacity() {return size_type(end_of_storage - begin());}bool empty() const {return begin() == end();}reference operator[](size_type n) {return *(begin() + n);}//constructorvector(): 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());}//end of constructor~vector() {destroy(start, finish);deallocate();}reference front() const {return *begin();}reference back() const {return *(end() - 1);}void push_back(const T &x) {if(finish != end_of_storage) {construct(finish, x);++finish;} else {insert_aux(end(), x);}}void insert(iterator position, size_type n, const T &x);void pop_back() {--finish;destroy(finish);}iterator erase(iterator pos) {if(pos+1 != end()) {copy(pos+1, finish, pos);}--finish;destroy(finish);return pos;}iterator erase(iterator first, iterator last) {iterator i = copy(last, finish, first);destroy(i, finish);finish = finish - (last - first);return first;}void clear() {erase(begin(), end());}};template <typename T, typename Alloc>void vector<T, Alloc>::insert_aux(iterator position, const T &x) {if(finish != end_of_storage) {//还有备用空间construct(finish, *(finish-1));++finish;T x_copy = x;copy_backward(position, finish-2, finish-1);*position = x_copy;} else {//已无备用空间const size_type old_size = size();//如果原大小为0,则配置1个元素,如果原大小不为0,则配置原大小的两倍const size_type len = old_size!=0 ? 2*old_size : 1;iterator new_start = alloc.allocate(len);iterator new_finish = new_start;try {//将源vector的内容拷到新vector(前段)new_finish = uninitialized_copy(start, position, new_start);//插入新元素construct(new_finish, x);++new_finish;//将后段内容拷过来new_finish = uninitialized_copy(position, finish, new_finish);} catch(...) {//commit or rollbackdestroy(new_start, new_finish);alloc.deallocate(new_start, len);throw;}//析构并释放原vectordestroy(begin(), end());deallocate();//调整迭代器start = new_start;finish = new_finish;end_of_storage = new_start + len;}}template <typename T, typename Alloc>void vector<T, Alloc>::insert(iterator position, size_type n, const T &x) {if(n != 0) {//当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();//新空间的长度 2倍 or 旧长度加新增元素个数const size_type len = old_size + max(old_size, n);iterator new_start = alloc.allocate(len);iterator new_finish = new_start;//旧position前的元素拷贝到新空间new_finish = uninitialized_copy(start, position, new_start);//插入新元素uninitialized_fill_n(new_finish, n, x);new_finish += n;//旧postion后的元素拷贝到新空间new_finish = uninitialized_copy(position, finish, new_finish);//清除并释放旧的空间destroy(start, finish);deallocate();//调整迭代器start = new_start;finish = new_finish;end_of_storage = new_start + len;}}}#endif

其中的construct(), destroy()函数参见 点击打开链接


测试代码:

//main.cpp#include "vector.h"#include <iostream>#include <algorithm>using std::cout;using std::endl;using std::find;using std::copy;using std::uninitialized_fill_n;using std::uninitialized_copy;using std::max;using std::fill;using std::copy_backward;int main(void) {size_t i;vector<int> ivec(1, 9);cout << "size=" << ivec.size() << endl;// size=1cout << "capacity=" << ivec.capacity() << endl;// capacity=1ivec.push_back(1);cout << "size=" << ivec.size() << endl;// size=2cout << "capacity=" << ivec.capacity() << endl;// capacity=2ivec.push_back(2);cout << "size=" << ivec.size() << endl;// size=3cout << "capacity=" << ivec.capacity() << endl;// capacity=4ivec.push_back(3);cout << "size=" << ivec.size() << endl;// size=4cout << "capacity=" << ivec.capacity() << endl;// capacity=4ivec.push_back(4);cout << "size=" << ivec.size() << endl;// size=5cout << "capacity=" << ivec.capacity() << endl;// capacity=8for(i=0; i<ivec.size(); i++) {cout << ivec[i] << " ";// 9 1 2 3 4}cout << endl;ivec.push_back(5);cout << "size=" << ivec.size() << endl;// size=6cout << "capacity=" << ivec.capacity() << endl;// capacity=8for(i=0; i<ivec.size(); i++) {cout << ivec[i] << " ";// 9 1 2 3 4 5}cout << endl;ivec.pop_back();ivec.pop_back();cout << "size=" << ivec.size() << endl;// size=4cout << "capacity=" << ivec.capacity() << endl;// capacity=8ivec.pop_back();cout << "size=" << ivec.size() << endl;// size=3cout << "capacity=" << ivec.capacity() << endl;// capacity=8vector<int>::iterator iter2 =find(ivec.begin(), ivec.end(), 1);if(iter2 != ivec.end()) {ivec.erase(iter2);}cout << "size=" << ivec.size() << endl;// size=2cout << "capacity=" << ivec.capacity() << endl;// capacity=8for(i=0; i<ivec.size(); i++) {cout << ivec[i] << " ";// 9 2}cout << endl;vector<int>::iterator iter1 =find(ivec.begin(), ivec.end(), 2);if(iter1 != ivec.end()) {ivec.insert(iter1, 3, 7);}cout << "size=" << ivec.size() << endl;// size=5cout << "capacity=" << ivec.capacity() << endl;// capacity=8for(i=0; i<ivec.size(); i++) {cout << ivec[i] << " ";// 9 7 7 7 2}cout << endl;ivec.clear();cout << "size=" << ivec.size() << endl;// size=0cout << "capacity=" << ivec.capacity() << endl;// capacity=8system("pause");return 0;}



原创粉丝点击