C/C++ 的使用
C++ http://www.cplusplus.com/
http://www.cplusplus.me/
*****************容器container vector
转自 http://blog.csdn.net/qscool1987/article/details/7050487
- #ifndef _MY_VECTOR_H
- #define _MY_VECTOR_H
- #include <string.h>
- #include <assert.h>
- template<class T>
- class MyVector
- {
- public:
- class iterator
- {
- public:
- iterator():pelem(NULL){}
- iterator(T *pt):pelem(pt){}
- iterator(const iterator &iter);
- iterator& operator = (const iterator &iter);
- iterator& operator = (T *pt);
- ~iterator(){}
- bool operator != (const iterator &iter);
- iterator& operator ++ ();
- iterator& operator ++ (int);
- iterator& operator -- ();
- iterator& operator -- (int);
- iterator operator + (size_t size);
- iterator operator - (size_t size);
- iterator& operator -= (size_t size);
- iterator& operator += (size_t size);
- T& operator * ();
- //functions add here
- private:
- T *pelem;
- };
- //constructor
- MyVector():pbuff(NULL),beg(NULL),last(NULL),count(0),capcity(0){}
- MyVector(const MyVector &orig);
- MyVector& operator = (const MyVector &orig);
- ~MyVector();
- //member function
- T& operator [] (size_t index);
- void pushback(const T &mt);
- iterator insert(size_t index,const T &mt);
- iterator insert(const T *phead,const T *pback, iterator p);
- iterator erase(size_t index);
- iterator erase(iterator phead, iterator pback);
- void clear();
- size_t size();
- size_t capacity();
- iterator begin();
- iterator end();
- private:
- void del_buff()
- {
- if(NULL != pbuff)
- {
- delete pbuff;
- pbuff = NULL;
- }
- }
- T *pbuff;//Memory buff for elements
- iterator beg;
- iterator last;
- size_t count;
- size_t capcity;
- };
- /**MyVector's member functions**/
- /**here are the member functions implementations**/
- template<class T>
- size_t MyVector<T>::size()
- {
- return count;
- }
- template<class T>
- size_t MyVector<T>::capacity()
- {
- return capcity;
- }
- template<class T>
- MyVector<T>::MyVector(const MyVector<T> &orig)
- {
- count = orig.size();
- capcity = 2*count;
- pbuff = new T [count*2];
- size_t totalbytes = count*2*sizeof(T);
- memcpy(pbuff,orig.pbuff,totalbytes);
- }
- template<class T>
- MyVector<T>& MyVector<T>::operator = (const MyVector<T> &orig)
- {
- del_buff();
- count = orig.size();
- capcity = 2*count;
- pbuff = new T [count*2];
- size_t totalbytes = count*2*sizeof(T);
- memcpy(pbuff,orig.pbuff,totalbytes);
- return *this;
- }
- template<class T>
- MyVector<T>::~MyVector<T>()
- {
- del_buff();
- }
- template<class T>
- T& MyVector<T>::operator[](size_t index)
- {
- return pbuff[index];
- }
- template<class T>
- void MyVector<T>::pushback(const T &mt)
- {
- if(NULL == pbuff && 0 == count)
- {
- pbuff = new T[(1+count)*2];
- pbuff[0] = mt;
- count++;
- capcity = 2*count;
- }
- else
- {
- if(NULL != pbuff && count == capcity)
- {
- capcity *= 2;
- T *ptem = new T[capcity];
- size_t totalbytes = capcity*sizeof(T);
- memcpy(ptem,pbuff,totalbytes);
- del_buff();
- pbuff = ptem;
- pbuff[count] = mt;
- count ++;
- }
- if(NULL != pbuff && count != capcity)
- {
- pbuff[count] = mt;
- count++;
- }
- }
- }
- template<class T>
- typename MyVector<T>::iterator MyVector<T>::insert(size_t index,const T &mt)
- {
- assert(count >= index);
- if(NULL != pbuff && count == capcity)
- {
- capcity *= 2;
- T *ptem = new T[capcity];
- memcpy(ptem,pbuff,capcity*sizeof(T));
- ptem[index] = mt;
- memcpy(&ptem[index+1],(count-index)*sizeof(T));
- del_buff();
- pbuff = ptem;
- count ++;
- typename MyVector<T>::iterator _iter(&pbuff[index]);
- return _iter;
- }
- else if(NULL != pbuff && count != capcity)
- {
- size_t _end = count-1;
- size_t _beg = index;
- for(;_end >= _beg;_end--)
- pbuff[_end+1] = pbuff[_end];
- pbuff[index] = mt;
- count++;
- typename MyVector<T>::iterator _iter(&pbuff[index]);
- return _iter;
- }
- }
- template<class T>
- typename MyVector<T>::iterator MyVector<T>::
- insert(const T *phead,const T *pback,
- typename MyVector<T>::iterator p)
- {
- typename MyVector<T>::iterator _beg = begin(),_end = end();
- size_t insertnum = 0;
- for(;phead != pback;phead++)
- insertnum++;
- phead -= insertnum;
- size_t index = 0;
- for(;_beg != p;_beg++)
- index++;
- if(count +insertnum > capcity && NULL != pbuff)
- {
- capcity = 2*(count +insertnum);
- T *ptem = new T [capcity];
- memcpy(ptem,pbuff,(index)*sizeof(T));
- memcpy(&ptem[index],phead,insertnum*sizeof(T));
- memcpy(&ptem[index+insertnum],&pbuff[index],
- (count-index)*sizeof(T));
- del_buff();
- pbuff = ptem;
- count += insertnum;
- typename MyVector<T>::iterator _iter(&pbuff[index]);
- return _iter;
- }
- else if(count +insertnum <= capcity && NULL != pbuff)
- {
- for(size_t i = insertnum;i != 0;i--,count--)
- pbuff[count+insertnum-1] = pbuff[count-1];
- for(;phead != pback;phead++,p++)
- *p = *phead;
- count += insertnum;
- return p;
- }
- if(NULL == pbuff && 0 == count)
- {
- capcity = 2*insertnum;
- pbuff = new T[capcity];
- memcpy(pbuff,phead,insertnum*sizeof(T));
- count = insertnum;
- typename MyVector<T>::iterator _iter(&pbuff[0]);
- return _iter;
- }
- }
- template<class T>
- typename MyVector<T>::iterator MyVector<T>::erase(size_t index)
- {
- T *temp = new T[count-index-1];
- memcpy(temp,&pbuff[index+1],(count-index-1)*sizeof(T));
- memcpy(&pbuff[index],temp,(count-index-1)*sizeof(T));
- pbuff[count-1] = '\0';
- count--;
- delete [] temp;
- typename MyVector<T>::iterator _iter(&pbuff[index]);
- return _iter;
- }
- template<class T>
- typename MyVector<T>::iterator MyVector<T>::
- erase( typename MyVector<T>::iterator phead,
- typename MyVector<T>::iterator pback)
- {
- size_t elemnum = 0;
- size_t _toend = 0;
- for(;phead != pback;phead++)
- elemnum++;
- phead -= elemnum;
- for(;pback != end();pback++)
- _toend++;
- pback -= _toend;
- T *temp = new T[_toend];
- memcpy(temp,&pbuff[count-_toend],_toend*sizeof(T));
- memcpy(&pbuff[count-elemnum-_toend],temp,_toend*sizeof(T));
- memset(&pbuff[count-elemnum],'\0',(count-elemnum)*sizeof(T));
- delete [] temp;
- count -= elemnum;
- return phead;
- }
- template<class T>
- typename MyVector<T>::iterator MyVector<T>::begin()
- {
- beg = &pbuff[0];
- return beg;
- }
- template<class T>
- typename MyVector<T>::iterator MyVector<T>::end()
- {
- last = &pbuff[count];
- return last;
- }
- /**nested dependent typeclass**/
- /**implementation**/
- template<class T>
- MyVector<T>::iterator::iterator(const typename MyVector<T>::iterator &iter)
- {
- pelem = iter.pelem;
- }
- template<class T>
- typename MyVector<T>::iterator& MyVector<T>::iterator::
- operator = (const typename MyVector<T>::iterator &iter)
- {
- pelem = iter.pelem;
- return *this;
- }
- template<class T>
- typename MyVector<T>::iterator& MyVector<T>::iterator::
- operator = (T *pt)
- {
- pelem = pt;
- return *this;
- }
- template<class T>
- bool MyVector<T>::iterator::operator !=
- (const typename MyVector<T>::iterator &iter)
- {
- if(pelem != iter.pelem)
- return true;
- else
- return false;
- }
- template<class T>
- typename MyVector<T>::iterator& MyVector<T>::iterator::operator ++ ()
- {
- ++pelem;
- return *this;
- }
- template<class T>
- typename MyVector<T>::iterator& MyVector<T>::iterator::operator ++ (int)
- {
- pelem++;
- return *this;
- }
- template<class T>
- typename MyVector<T>::iterator& MyVector<T>::iterator::operator -- (int)
- {
- pelem--;
- return *this;
- }
- template<class T>
- typename MyVector<T>::iterator& MyVector<T>::iterator::operator -- ()
- {
- --pelem;
- return *this;
- }
- template<class T>
- typename MyVector<T>::iterator& MyVector<T>::iterator::operator += (size_t size)
- {
- pelem += size;
- return *this;
- }
- template<class T>
- typename MyVector<T>::iterator& MyVector<T>::iterator::operator -= (size_t size)
- {
- pelem -= size;
- return *this;
- }
- template<class T>
- typename MyVector<T>::iterator MyVector<T>::iterator::operator + (size_t size)
- {
- pelem += size;
- typename MyVector<T>::iterator _iter(pelem);
- return _iter;
- }
- template<class T>
- typename MyVector<T>::iterator MyVector<T>::iterator::operator - (size_t size)
- {
- pelem -= size;
- typename MyVector<T>::iterator _iter(pelem);
- return _iter;
- }
- template<class T>
- T& MyVector<T>::iterator::operator * ()
- {
- return *pelem;
- }
- #endif
随机推荐
- [luogu2964][USACO09NOV][硬币的游戏A Coin Game] (博弈+动态规划)
题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...
- Swift中的willSet与didSet
Swift中的willSet与didSet 周银辉 在Swift语言中用了willSet和didSet这两个特性来监视属性的除初始化之外的属性值变化 无需说太多,看看下面的代码你就能很快明白的 imp ...
- 微信小程序开发工具使用与设计规范(二)
[未经作者本人同意,请勿以任何形式转载] 上一篇文章主要分析了微信小程序应用场景和优劣势.本篇你可以学习到: 如何使用小程序开发工具写一个Hello World 微信小程序设计规范 微信小程序项目结构 ...
- 如何重新划分linux分区大小
1.下载脚本文件,将脚本文件内容复制 chmod +x resize.sh sudo ./resize.sh 输入上面命令后会看到下面的结果 root@odroid:~# sudo ./resize. ...
- ElasticSearch第一步-环境配置
ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSearch第四步-查询详解 Elasti ...
- jquery easy ui datagrid中遇到的坑爹的問題。。。
; //修改 function Update() { //獲取選中行 var selected = $("#dg1").datagrid('getSelected'); //判斷是 ...
- Gson解析json字符串
// 解析传递过来的json字符串 JsonParser parser = new JsonParser(); JsonObject jsonObj = parser.parse(strJson).g ...
- 如何指定个别属性进行transition过渡
transition是CSS3新增的动画属性,可以实现属性的平滑过渡,大大提高用户体验,对于多个属性进行过渡的话很多人会这样写 .tr{ transition:all 1s} 很不幸的是如果我只需要对 ...
- Python基础-三次用户验证登录购买商品程序
需求: 一:三次登录锁定 1.用户信息存放于文件中 2.尝试三次都失败,锁定用户 二.购物车功能要求: 要求用户输入总资产,例如:2000显示商品列表,让用户根据序号选择商品,加入购物车购买,如果商品 ...
- Matlab2015入门学习01
1. 两个命令: clear: 清除内存中变量的值(在workspace中可以看到) clc: 清除Command Window中的输出 2. 脚本编辑器: matlab脚本扩展名为*.m 在Comm ...