如何重载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. 从零开始学Electron笔记(三)

    在之前的文章我们介绍了一下Electron的菜单创建和事件绑定,其中提到了一个remote模块,接下来我们继续说一下Electron的这个remote模块. 官方关于remote模块的地址:https ...

  2. day27 面向对象

    day27 面向对象 目录 day27 面向对象 一.面相对象介绍 1 什么是对象 2 类于对象 二.实现面向对象编程 1 先定义类 2 属性访问 2.1 调用dict方法 2.2 类.属性 3 调用 ...

  3. day17 作业

    目录 一.编写函数(函数执行的时间用time.sleep(n)模拟) 二.编写装饰器,为函数加上统计时间的功能 三.编写装饰器,为函数加上认证的功能 四.编写装饰器,为多个函数加上认证的功能(用户的账 ...

  4. 【Nginx】如何获取客户端真实IP、域名、协议、端口?看这一篇就够了!

    写在前面 Nginx最为最受欢迎的反向代理和负载均衡服务器,被广泛的应用于互联网项目中.这不仅仅是因为Nginx本身比较轻量,更多的是得益于Nginx的高性能特性,以及支持插件化开发,为此,很多开发者 ...

  5. [系列] Go - json.Unmarshal 遇到的小坑

    1.问题现象描述 使用 json.Unmarshal(),反序列化时,出现了科学计数法,参考代码如下: jsonStr := `{"number":1234567}` result ...

  6. CRLF injection 简单总结

    CRLF injection 简单总结 简介 CRLF是"回车 + 换行"(\r\n)的简称,即我们都知道在HTTP协议中,HTTP Header与HTTP Body是用两个CRL ...

  7. linux专题(一):小白的开始以及相关的学习链接

    转载自:https://www.cnblogs.com/ggjucheng/archive/2011/12/16/2290158.html 学习Linux也有一阵子了,这过程中磕磕撞撞的,遇到了问题, ...

  8. 从零开始学Electron笔记(五)

    在之前的文章我们介绍了一下Electron的右键菜单的制作,接下来我们继续说一下Electron如何通过链接打开浏览器和嵌入网页. 现在有这样一个需求,我们要在我们的软件中加一个链接,然后点击该链接打 ...

  9. linux 安装superset

    背景说明 公司数据分析人员需要将日常监控分析数据进行可视化,在踩了一些坑之后,终于在业务环境中搭建成功superset,后续复现两次流程也是成功的,分享一波... 业务环境说明 操作系统:centos ...

  10. Mysql数据库搭建集群---实现主从复制,读写分离

    参考博客:https://blog.csdn.net/xlgen157387/article/details/51331244 A.  准备:多台服务器,且都可以互相随意访问root用户,都可以随意进 ...