简单的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的大小,以及容量(容量的改变是通过为基本数组分配一个新的内存块,然后复制 ...
随机推荐
- Python OpenCV的绘图功能简介
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:大Z 在图像中我们经常需要用到将某个局部特征画出来,比如物体检测,物 ...
- day06总结
字符串常用操作# ======================================基本使用======================================# 1.用途:记录描述 ...
- 全局作用域中,用 const 和 let 声明的变量不在 window 上,那到底在哪里?如何去获取?
在ES5中,顶层对象的属性和全局变量是等价的,var 命令和 function 命令声明的全局变量,自然也是顶层对象. var a = 12; function f(){}; console.log( ...
- CMDB03 /今日未采集的资产、资产入库、资产变更记录、资产采集
CMDB03 /今日未采集的资产.资产入库.资产变更记录.资产采集 目录 CMDB03 /今日未采集的资产.资产入库.资产变更记录.资产采集 1. 获取今日未采集的服务器 2. server资产入库以 ...
- Navicat连接数据库报错2059 - authentication plugin...错误解决方法
今天使用Navicat 连接MySQL数据库出现错误:2059 - authentication plugin 'caching_sha2_password'. 出现这个错误的原因是因为MySQL8. ...
- JSON基础使用详解
JSON:JavaScript对象表示法 1.1 JSON说明 曾经一段时间,XML是互联网传递数据的统一标准,但是业界一直不乏质疑XML的人士,他们都认为XML过于繁琐,冗长:提取数据也过于麻烦 2 ...
- bzoj3437小P的牧场
bzoj3437小P的牧场 题意: n个牧场,在每个牧场见控制站的花费为ai,在该处建控制站能控制从此处到左边第一个控制站(或边界)之间的牧场.一个牧场被控制的花费等于它到控制它的控制站之间的牧场数目 ...
- 前端框架-jQuery自学笔记
What's jQuery jq就是一个封装了很多方法的js库. Why use jQuery 原生js的缺点 不能添加多个入口函数(window.onload),如果添加多个,后面会把前面的覆盖 a ...
- html-webpack-plugin在html中插入数据
html-webpack-plugin在html中插入数据 <!DOCTYPE html> <html> <head> <meta charset=" ...
- T133316 57级返校测试重测-T4-字符串的修改
大致题意: 有一个A字符串和一个B字符串, 操作将A或A的一个后缀修改为B, 求最少的操作数. 有三个操作为: 删除: 删除掉 A 中的某一个字符. 添加: 将某一个字符添加到 A 中任意位置. 替换 ...