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
随机推荐
-
js屏幕尺寸 笔记
"屏幕分辨率为:"+screen.width+"*"+screen.height "屏幕可用大小:"+screen.availWidth+& ...
-
poj 1753 Flip Game
点击打开链接 Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25674 Accepted: 1109 ...
-
R 中安装xlsx包缺少java环境解决方案
1.安装Java程序(官网win7 64位系统的Java安装程序及网址http://www.java.com/zh_CN/download/manual.jsp),选择windows 64位脱机安装到 ...
-
windows下bat批处理实现守护进程(有日志)
开发部的一个核心程序总是会自己宕机,然后需要手工去起,而这个服务的安全级别又很高,只有我可以操作,搞得我晚上老没法睡,昨晚实在受不了了,想起以前在hp-ux下写的shell守护进程,这回搞个windo ...
-
数据分页SQL语句的比较
建立表 CREATE TABLE [TestTable] ( , ) NOT NULL , ) COLLATE Chinese_PRC_CI_AS NULL , ) COLLATE Chinese_P ...
-
VM Agent 和扩展程序
VM Agent 和扩展程序 - 第 1 部分 Windows Azure基础结构服务最近宣布了一项新功能VM Agent.VMAgent是一个轻量级进程,用于启动由Microsoft或合作伙伴 ...
-
OpenCV imread读取jpg图像的一个大坑
长话短说 版本区间[OpenCV3.0.0, OpenCV3.4.1]内的OpenCV,(至少在windows下,使用官方提供的预编译版本),imread读取jpg图片后的像素值,和版本区间[Open ...
-
Failed to decode downloaded font
碰到如下错误,该错误是开启layui的打印.导出.筛选列时出现的,不能正常显示图标及文字 原因: @参考文章 因为经过maven的filter,会破坏font文件的二进制文件格式,到时前台解析出错 解 ...
-
grid++json页面数据传入
最近遇到一个问题,就是要用Grid++做页面数据报表打印,但是翻了Grid++文档就是没有直接从页面上传数据的,都是要加载txt文档,填写txt文档的url.自己尝试直接页面上传JSON数据到Grid ...
-
web项目报outmemory错误解决方案
因为数据问题内存不够出现错误,将参数加入到eclipse的run的配置文件中: