简单的vector--- 2
如何重载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的更多相关文章
- 转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
转载自:http://www.cnblogs.com/cj695/p/3863142.html sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在 ...
- 【转】 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...
- 【C++】从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...
- 自己动手实现简单的Vector
看到今天,终于自己动手写了一个自己的vector,我这个版本的vector只有vector主要的一些操作,包括原版vector的所有构造函数,begin(),end(),size(),capacity ...
- C++中STL中简单的Vector的实现
该vector只能容纳标准库中string类, 直接上代码了,StrVec.h文件内容为: #ifndef STRVEC_H #define STRVEC_H #include<iostream ...
- vc++简单的vector动态数组实现
#ifndef __MYVECTOR__ #define __MYVECTOR__ #include <Windows.h> #define SUCCESS 1 // 成功 #define ...
- 简单的 vector
#pragma once #include <memory.h> #include <stdlib.h> #include <iostream> using std ...
- C++线性序列容器<vector>简单总结
C++线性序列容器<vector>简单总结 vector是一个长度可变的数组,使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加:Vector类提供额外的方法来增加.删除 ...
- 线性表实现简单vector
实现一个简单的vector Vector基于数组实现,可以复制并且其占用的内存可以自动回收(通过析构函数),可以调整Vector的大小,以及容量(容量的改变是通过为基本数组分配一个新的内存块,然后复制 ...
随机推荐
- C#中Linq查询List与DataTable和Dictionary
查询DataTable返回List List<string> listNation = dtNation.AsEnumerable().Select(d => d.Field< ...
- WPF弹性、惯性效果应用
WPF弹性.惯性效果.已发布在了一些网站,都是这里写的 代码改编自微软示例代码库 // Copyright © Microsoft Corporation. All Rights Reserved. ...
- 万变不离其宗之UART要点总结
[导读] 单片机开发串口是应用最为广泛的通信接口,也是最为简单的通信接口之一,但是其中的一些要点你是否明了呢?来看看本人对串口的一些总结,当然这个总结并不能面面俱到,只是将个人认为具有共性以及相对比较 ...
- 浅析Python垃圾回收机制!
Python垃圾回收机制 目录 Python垃圾回收机制 1. 内存泄露 2. Python什么时候启动垃圾回收机制? 2.1 计数引用 2.2 循环引用 问题:引用计数是0是启动垃圾回收的充要条件吗 ...
- Maven 专题(七):常用命令
mvn archetype:generate : 反向生成项目的骨架 mvn clean: 清除各个模块target目录及里面的内容 mvn compile: 静态编译,根据xx.java生成xx.c ...
- 数据可视化之powerBI技巧(十六)采悟:PowerBI作图技巧:动态显示可视化标题
默认情况下,PowerBI图表的标题是静态的,为了增强图表的可读性,通过设置动态标题,可快速展示关键信息.提升沟通效率.本文通过两个简单的例子来看看PowerBI中如何创建动态标题. /01/ 拿之前 ...
- python 并发专题(十三):asyncio (一) 初识
https://www.cnblogs.com/wongbingming/p/9095243.html . 本文目录# 如何定义/创建协程 asyncio的几个概念 学习协程是如何工作的 await与 ...
- bzoj2697特技飞行*
bzoj2697特技飞行 题意: N个单位时间,每个单位时间可以进行一项特技动作,可选的动作有K种,每种动作有一个刺激程度Ci.每次动作的价值为(距上次该动作的时间)*Ci,若为第一次进行该动作,价值 ...
- tcpreplay的使用指导
Tcpreplay的介绍 简单的说, tcpreplay是一种pcap包的重放工具, 它可以将用ethreal, wireshark工具抓下来的包原样或经过任意修改后重放回去. 它允许你对报文做任意的 ...
- React js ReactDOM.render 语句后面不能加分号
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...