如何重载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. .NET程序运行原理及基本概念详解

    一.引言 我们知道在Java中有虚拟机,代码运行时虚拟机把Java语言编译成与机器无关的字节码,然后再把字节码编译成机器指令执行,那么在.NET中程序是如何运行的呢?其实运行原理是一样的,.NET中的 ...

  2. 根据URL下载图片到本地

    /// <summary> /// 下载图片 /// </summary> /// <param name="picUrl">图片Http地址& ...

  3. SpringBoot日志功能

    三.SpringBoot日志功能 1.日志框架 市面上的日志框架: JUL.JCL.Jboss-logging.Logback.Log4j.Log4j.SLF4J... 日志门面(日志的抽象层) 日志 ...

  4. Spring源码解析——核心类介绍

    前言: Spring用了这么久,虽然Spring的两大核心:IOC和AOP一直在用,但是始终没有搞懂Spring内部是怎么去实现的,于是决定撸一把Spring源码,前前后后也看了有两边,很多东西看了就 ...

  5. Python-02 可视化之tkinter介绍

    1 控件介绍 1.1 Label import tkinter as tk # 使用Tkinter前需要先导入 window = tk.Tk() window.title('My Window') w ...

  6. java 面向对象(十四):面向对象的特征二:继承性 (三) 关键字:super以及子类对象实例化全过程

    关键字:super 1.super 关键字可以理解为:父类的2.可以用来调用的结构:属性.方法.构造器3.super调用属性.方法:3.1 我们可以在子类的方法或构造器中.通过使用"supe ...

  7. 数据分析04 /基于pandas的DateFrame进行股票分析、双均线策略制定

    数据分析04 /基于pandas的DateFrame进行股票分析.双均线策略制定 目录 数据分析04 /基于pandas的DateFrame进行股票分析.双均线策略制定 需求1:对茅台股票分析 需求2 ...

  8. L1和L2正则化。L1为什么能产生稀疏值,L2更平滑

    参考博客:https://zhuanlan.zhihu.com/p/35356992 https://zhuanlan.zhihu.com/p/25707761 https://www.zhihu.c ...

  9. [译]使用DOT语言和GraphvizOnline来可视化你的ASP.NETCore3.0终结点01

    这是系列文章中的第一篇:使用GraphvizOnline可视化ASP.NETCore3.0终结点.. 第1部分-使用DOT语言来可视化你的ASP.NETCore3.0终结点(本文) 第2部分-向ASP ...

  10. koa中是如何封装获取客户端IP的?

    案例 var koa = require('koa') var app = new koa() app.use(function (ctx,next) { ctx.body = ctx.ip }) a ...