如何重载operator[]   及其相关细节

如何使用 const_cast<>(  )  和 static_cast<>( )

模板类 如何内部声明,外部定义友元函数

使用memset( )、memcpy_s( )

使用sizeof( )

禁用移动构造 和 移动赋值

 #pragma once
#include <iostream>
using std::ostream;
using std::cout;
using std::endl; template <class T>
class vector
{
public:
explicit vector(int size);
vector(const vector<T>& v);
vector(vector<T>&&) = delete;
vector(int size, T vlaue);
vector(T* es, int len);
vector& operator=(const vector<T>& v);
vector& operator=(vector<T>&&) = delete; int size() const;
int capacity() const;
void reserve(int min_capacity);
void push_back(T e);
void pop_back();
void erase(int pos);
void insert(T e, int pos); const T& operator[](int) const;
T& operator[](int);
template <class Ty>
friend ostream& operator<<(ostream& os, const vector<Ty>& v);
private:
T* vec;
int _size;
int _capacity;
}; template <class T>
inline ostream& operator<<(ostream& os, const vector<T>& v)
{
os << endl << "输出: " << endl;
for (int i = ; i < v._size; i++)
{
os << v.vec[i] << " ";
}
return os;
} template <class T>
inline vector<T>::vector(int size) : _size(size)
{
_capacity = size + size / ;
this->vec = new T[_capacity];
memset(this->vec, 0x00, sizeof(T) * _capacity);
} template <class T>
inline vector<T>::vector(int size, T vlaue)
{
this->_capacity = size + size / ;
this->_size = size;
this->vec = new T[_capacity];
for (int i = ; i < _size; i++)
{
this->vec[i] = vlaue;
}
} template <class T>
inline vector<T>::vector(T* es, int len)
{
this->_size = len;
this->_capacity = len + len / ;
this->vec = new T[_capacity];
memcpy_s(this->vec, len * sizeof(T), es, len * sizeof(T));
} template <class T>
inline vector<T>::vector(const vector& v)
{
this->_capacity = v._capacity;
this->_size = v._size;
this->vec = new T[_capacity];
memcpy_s(this->vec, sizeof(T) * _capacity, v.vec, sizeof(T) * _capacity);
} template <class T>
inline int vector<T>::size() const
{
return this->_size;
} template <class T>
inline int vector<T>::capacity() const
{
return this->_capacity;
} template <class T>
inline vector<T>& vector<T>::operator=(const vector<T>& v)
{
if (this == &v)return *this;
if (this->vec != nullptr)delete[] this->vec;
this->_capacity = v._capacity;
this->_size = v._size;
this->vec = new T[_capacity];
memcpy_s(this->vec, _capacity * sizeof(T), v.vec, v._capacity * sizeof(T));
return *this;
} template <class T>
inline void vector<T>::reserve(const int min_capacity)
{
if (min_capacity < this->_capacity) return;
else
{
int* n_vec = new T[min_capacity];
for (int i = ; i < _size; i++)
{
n_vec[i] = vec[i];
}
delete[] vec;
this->vec = n_vec;
this->_capacity = min_capacity;
}
} template <class T>
inline void vector<T>::push_back(T e)
{
if (this->_size >= this->_capacity)
{
this->reserve(this->_size + this->_size / );
}
this->vec[_size++] = e;
} template <class T>
inline void vector<T>::pop_back()
{
if (this->_size != )
this->_size -= ;
} template <class T>
inline void vector<T>::erase(int pos)
{
if (pos < || pos >= _size)return;
else
{
for (int i = pos; i < _size - ; i++)
{
this->vec[i] = this->vec[i + ];
}
_size -= ;
}
} template <class T>
inline void vector<T>::insert(T e, int pos)
{
if (pos < || pos > _size - )return;
if (this->_size >= this->_capacity)
{
this->reserve(this->_size + this->_size / );
}
for (int i = _size; i > pos; i++)
{
this->vec[i] = this->vec[i - ];
}
this->vec[pos] = e;
} template <class T>
inline const T& vector<T>::operator[](int i) const
{
if (i < || i >= this->_size)
{
cout << "下标 i=" << i << " 越界 退出";
exit();
};
return this->vec[i];
} template <class T>
T& vector<T>::operator[](int pos)
{
// 这个写法的意思是 将指针this 转成 const 型指针 再对其*解引
//return const_cast<T&>((*static_cast<const vector<T>*>(this))[pos]); //这个是先对this 指针解引 再转成const 而且&直接引用该对象
return const_cast<T&>(static_cast<const vector<T>&>(*this)[pos]); //这个写法不不可取;先对this指针 解引,再转成const时 并不是&,所以会重新创建一个新对象
//return const_cast<T&>(static_cast<const vector<T>>(*this)[pos]);
}

简单的vector--- 2的更多相关文章

  1. 转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    转载自:http://www.cnblogs.com/cj695/p/3863142.html sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在 ...

  2. 【转】 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...

  3. 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...

  4. 【C++】从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...

  5. 自己动手实现简单的Vector

    看到今天,终于自己动手写了一个自己的vector,我这个版本的vector只有vector主要的一些操作,包括原版vector的所有构造函数,begin(),end(),size(),capacity ...

  6. C++中STL中简单的Vector的实现

    该vector只能容纳标准库中string类, 直接上代码了,StrVec.h文件内容为: #ifndef STRVEC_H #define STRVEC_H #include<iostream ...

  7. vc++简单的vector动态数组实现

    #ifndef __MYVECTOR__ #define __MYVECTOR__ #include <Windows.h> #define SUCCESS 1 // 成功 #define ...

  8. 简单的 vector

    #pragma once #include <memory.h> #include <stdlib.h> #include <iostream> using std ...

  9. C++线性序列容器<vector>简单总结

    C++线性序列容器<vector>简单总结 vector是一个长度可变的数组,使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加:Vector类提供额外的方法来增加.删除 ...

  10. 线性表实现简单vector

    实现一个简单的vector Vector基于数组实现,可以复制并且其占用的内存可以自动回收(通过析构函数),可以调整Vector的大小,以及容量(容量的改变是通过为基本数组分配一个新的内存块,然后复制 ...

随机推荐

  1. 我终于弄懂了Python的装饰器(一)

    此系列文档: 1. 我终于弄懂了Python的装饰器(一) 2. 我终于弄懂了Python的装饰器(二) 3. 我终于弄懂了Python的装饰器(三) 4. 我终于弄懂了Python的装饰器(四) 一 ...

  2. LoadLibraryA与GetProcAddress介绍

    0x00 函数原型 HMODULE LoadLibraryA(     LPCTSTR lpLibFileName//模块的的的名字 ) FARPROC GetProcAddress( HMODULE ...

  3. docker自动化部署前端项目实战一

    docker自动化部署前端项目实战一 本文适用于个人项目,如博客.静态文档,不涉及后台数据交互,以部署文档为例. 思路 利用服务器node脚本,监听github仓库webhook push事件触发po ...

  4. Dot Net Core中间件内部运行机制

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { ...

  5. Github 新玩法 -- Profile ReadMe

    Github 新玩法 -- Profile ReadMe Intro 今天刷 Github 的时候偶然发现一个新的玩法,Github Profile ReadMe,可以在个人的 Profile 页面展 ...

  6. Mysql基础(十一):Self Join

    Summary: 如何使用 MySQL self join 进行表的 自己对自己的join操作.. 前面的教程,已经教过join语法,都是两个表的之间的操作,特殊的,当一个表自己和自己进行join,那 ...

  7. 数据可视化基础专题(十二):Matplotlib 基础(四)常用图表(二)气泡图、堆叠图、雷达图、饼图、

    1 气泡图 气泡图和上面的散点图非常类似,只是点的大小不一样,而且是通过参数 s 来进行控制的,多的不说,还是看个示例: 例子一: import matplotlib.pyplot as plt im ...

  8. 机器学习实战基础(三十七):随机森林 (四)之 RandomForestRegressor 重要参数,属性与接口

    RandomForestRegressor class sklearn.ensemble.RandomForestRegressor (n_estimators=’warn’, criterion=’ ...

  9. bzoj2132圈地计划

    bzoj2132圈地计划 题意: 一块土地可以纵横划分为N×M块小区域.于第i行第j列的区域,建造商业区将得到Aij收益,建造工业区将得到Bij收益.而如果区域(i,j)相邻(相邻是指两个格子有公共边 ...

  10. 将python3打包成为exe可执行文件(pyinstaller)

    我们工作中可能会遇到,客户需要一个爬虫或者其他什么功能的python脚本. 这个时候,如果我们直接把我们的python脚本发给客户,会有两个问题: 1.客户的电脑或者服务器可能并没有安装python环 ...