首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

自个儿写一个数组,同样也可以写一个容器

2012-08-16 
自己写一个数组,同样也可以写一个容器#includeiostream#includestring#includecstddef#includefunc

自己写一个数组,同样也可以写一个容器

#include<iostream>#include<string>#include<cstddef>#include<functional>#include<algorithm>using namespace std;template<typename T,std::size_t thesize>class carray{private:T v[thesize];public:typedef T value_type;typedef T* iterator;typedef const T* const_iterator;typedef T& reference;typedef const T& const_reference;typedef std::size_t size_type;typedef std::ptrdiff_t difference_type; iterator begin(){return &v[0];} const_iterator begin() const{return &v[0]; }iterator end(){return v+thesize;}const_iterator end() const{return v+thesize;}reference operator[](std::size_t i){return v[i];}const_reference operator[](std::size_t i) const{return v[i];}size_type size() const{return thesize;}size_type max_size(){return thesize;}T* as_array(){return v;}};int main(){carray<int,10>a;for(unsigned i=0;i<a.size();++i){a[i]=i+1;}for(unsigned i=0;i<a.size();++i){cout<<a[i]<<" ";}cout<<endl;reverse(a.begin(),a.end());for(unsigned j=0;j<a.size();++j){cout<<a[j]<<" ";}cout<<endl;transform(a.begin(),a.end(),a.begin(),negate<int>());for(int i=0;i<a.size();++i){cout<<a[i]<<" ";}cout<<endl;system("pause");return 0;}

自个儿写一个数组,同样也可以写一个容器

 

更详细的资料,参考boost网站的资料:看贴图

自个儿写一个数组,同样也可以写一个容器

模仿,自己写一个容器,有增删改函数、

热点排行