标签:c++ vector源代码实现
一:vector异常类 Myexcep.h
#include<string> #include<iostream> using namespace std; class Myexcep { public: Myexcep() :m_exnote("Get a exception ") {} Myexcep(const string &other){m_exnote = other;} virtual ~Myexcep(){} virtual void show_message() {cout << m_exnote <<endl;} private: string m_exnote; }; class Outofbond :public Myexcep { public: Outofbond() :m_outnote("Get a Out of bond exception ") {} Outofbond(const string &other){m_outnote = other;} ~Outofbond(){} void show_message(){cout << m_outnote <<endl;} private: string m_outnote; }; class Allocfail :public Myexcep { public: Allocfail():m_alonote("Get a Allocate fail exception "){} Allocfail(const string &other){m_alonote = other;} ~Allocfail(){} void show_message(){cout << m_alonote <<endl;} private: string m_alonote; };
#ifndef VEC_H_H #define VEC_H_H #include "Myexce.h" #include<cstdio> #include<iostream> #include<algorithm> #include<cassert> using namespace std; template <class T> class Vec { public: typedef T* iterator; typedef const T* const_iterator; typedef size_t size_type; typedef T value_type; public: Vec(); Vec(size_type,const T&); Vec(const Vec &); Vec& operator=(const Vec&); ~Vec();//调用相应的create()和uncreate()函数 T& operator[](size_type index); const T& operator[](size_type index) const;// 运算符重载 void push_back(const T&); void pop_back(); void erase(const size_type&);//根据下标删除元素,push_back()调用了grow() size_type size() const {return (m_finished - m_start);} iterator begin() {return m_start;} const_iterator begin() const {return m_start;} iterator end() {return m_finished;} const_iterator end() const {return m_finished;} private: void create(); void create(size_type,const T&); void create(const_iterator,const_iterator);//调用allocate()申请空间,并调用init_fill(),init_copy()进行初始化 void uncreate(); T* allocate(size_type); void init_fill(iterator,iterator,const T&); void init_copy(const_iterator,const_iterator,iterator); void grow();// 调用了size(),和 max() void append_element(const T&); private: iterator m_start; iterator m_finished; iterator m_limit;// 有这么多指针,肯定有深拷贝 }; #endif
//---------------------vec.cpp------------- #include "vec.h" /**** constructor ****/ template <class T> Vec<T>::Vec() { create(); } template <class T> Vec<T>::Vec(size_type sz,const T& val) { create(sz,val); } template <class T> Vec<T>::Vec(const Vec &rhs) { create(rhs.begin(),rhs.end()); } template <class T> Vec<T>& Vec<T>::operator=(const Vec& rhs) { if(&rhs == this) return *this; uncreate(); create(rhs.begin(),rhs.end()); return *this; } template <class T> Vec<T>::~Vec() { uncreate(); } /**** we have a create function to inital the data ****/ template <class T> void Vec<T>::create() { m_start = m_finished = m_limit = NULL; } template <class T> void Vec<T>::create(size_type sz,const T&val) { m_start = allocate(sz); m_finished = m_limit = m_start+sz; init_fill(m_start,m_limit,val); } template <class T> void Vec<T>::create(const_iterator first,const_iterator end) { size_type sz = end - first; m_start = allocate(sz); m_finished = m_limit = m_start+sz; init_copy(first,end,m_start); } template <class T> void Vec<T>::uncreate() { iterator iter = m_start; iterator end = m_limit; for(; iter != end; ) { iterator next_iter = iter + 1; delete(iter); iter = next_iter; } m_start = m_finished =m_limit =0; } /**** we define a function to allocate memory ****/ template <class T> T* Vec<T>::allocate(size_type sz) { T* t = new T[sizeof(T) * sz]; if(!t) { throw Allocfail(); } return t; } template <class T> void Vec<T>::init_fill(iterator data,iterator limit,const T& val) { iterator iter = data; iterator end = limit; for(; iter != end; iter++) { *iter = val; } } template <class T> void Vec<T>::init_copy(const_iterator first,const_iterator end,iterator lhs) { copy(first,end,lhs);// copy的实现暂时没有找到呢。。。 } // STL algorithm之copy template <class InputIterator, class OutputIterator> OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result ); //作用:将[first, last)范围的元素,拷贝到以result开始的范围内。类似于:memcopy() /**** overload operator [] ****/ template <class T> T& Vec<T>::operator[](size_type index) { if(index >= size()) { throw Outofbond(); } return m_start[index]; } template <class T> const T& Vec<T>::operator[](size_type index) const { if(index >= size()) { throw Outofbond(); } return m_start[index]; } /**** push_back ,pop_back,erase ****/ template <class T> void Vec<T>::push_back(const T& val) { if(m_finished == m_limit) grow(); append_element(val); } template <class T> void Vec<T>::pop_back() { if(size() == 0) return ; m_finished--; } template <class T> void Vec<T>::erase(const size_type& ipos) { if(ipos <0 || ipos >size()) { throw Outofbond(); } size_type iend = size()-1; size_type i = ipos; for(;i >= ipos && i < iend ; i++) { m_start[i] = m_start[i+1]; }// 后面的元素依次向前移动,只能删除一个 m_finished--; } template <class T> void Vec<T>::append_element(const T& val) { assert(m_finished!=m_start); *m_finished++ = val; } inline size_t max(const size_t lhs,const size_t rhs) { return lhs > rhs ? lhs:rhs; } template <class T> void Vec<T>::grow() { size_type new_size = max( 2*size(), 1); iterator new_start = allocate(new_size); iterator new_limit = new_start + new_size; iterator new_finished = new_start + size(); init_copy(begin(),end(),new_start); uncreate(); m_start = new_start; m_finished = new_finished; m_limit = new_limit; } /**** output the vector ****/ template <class T> ostream& operator<<(ostream &os, const Vec<T> &me) { const T* iter = me.begin(); //Is "const T*" replaced by "const_iterator"? for(;iter != me.end(); iter++) { os << *iter <<", "; } return os; } /**** test the case ****/ int main() { try { cout << "Test begin: " << endl; Vec<int> v1(4,3); cout << "After v1(4,3), v1[3]: " << v1[3] <<endl; Vec<int> v2(v1); cout << "After v2(v1), v2--value: " << v2 <<endl; Vec<int> v3; v3 = v2; cout << "After v3=v2, v3--value: " << v3 <<endl; v3.pop_back(); cout << "After v3.pop_back(), v3--value: " << v3 <<endl; v3.push_back(5); cout << "After v3.push_back(5), v3--value: " << v3 <<endl; Vec<double> v4(5, 222.2); cout << "After v4(5,222.2),v4--value:" << v4 << endl; Vec<char> v5(5, 'a'); cout << "After v5(5,222.2),v5--value:" << v5 << endl; string aa = "hi world"; // string bb = aa; // cout << bb << endl; Vec<string> v6(5, aa); v6.push_back("hello 好"); cout << v6 << endl; Vec<string> v7; v7 = v6; cout << v7[4] << endl; v3.erase(2); cout << "After v3.erase(2), v3--value: " << v3 <<endl; cout << "After v3.erase(2), v3--value: " << v3[6] <<endl; } catch(Outofbond e) { e.show_message(); exit(-1); } catch(Allocfail e){ e.show_message(); exit(-1); } return 0; }
vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组。像数组一样,vector类也用从0开始的下标表示元素的位置;但和数组不同的是,当vector对象创建后,数组的元素个数会随着vector对象元素个数的增大和缩小而自动变化。
1.构造函数
标签:c++ vector源代码实现
原文地址:http://blog.csdn.net/u010700335/article/details/39900231