#pragma once
#include <memory.h>
#include <stdlib.h>
#include <iostream>
using std::ostream;
using std::cout;
using std::endl;
class vector{
public :
vector();
~vector();
explicit vector(int size_);
vector(int size_,int e);
vector(const vector&a);
vector(int *data,int len);
vector& operator=(const vector&a);
vector(vector&&)=delete;
vector& operator=(vector&&)=delete; int size()const;
int capacity()const;
void reserve(int capacity_);
void resize(int size_);
int& operator[](int);
void insert(int pos,int x);//insert a new element before the position pos
void erase(int pos);//erase the element at the position pos;
void push_back(int x);//insert a new element at the end
void pop_back();//erase the last element
int& front();//return the first element
const int& front()const;
int& back();//return the last element
const int& back()const;
bool empty()const;//true if the size is 0
friend ostream& operator <<(ostream&os,const vector& a){
for(int i=;i<a._size;i++){
cout<<a.elems[i]<<" ";
}
cout<<endl;
return os;
} private:
int *elems;
int _size;
int _capacity;
bool _safePos(int a)const{
if(a<||a>_size-)return false;
else return true;
}
};
vector::vector(){
_size=;
_capacity=;
elems=nullptr;
}
vector::~vector(){
delete[] elems;
} vector::vector(int size_):_size(size_),_capacity(size_){
this->elems=new int[_capacity];
memset(this->elems,0x3f,_capacity*);
}
vector::vector(int size_, int e):_size(size_),_capacity(size_){
this->elems=new int[_capacity];
for(int i=;i<size_;i++){
this->elems[i]=e;
}
} vector::vector(const vector &a){
this->_size=a._size;
this->_capacity=a._capacity;
this->elems=new int[_capacity];
memcpy_s(this->elems,_capacity*,a.elems,_capacity*);
}
vector::vector(int *data, int len){
this->_capacity=len+len/;
this->_size=len;
this->elems=new int[_capacity];
memset(this->elems,0x3f,_capacity*);
memcpy_s(this->elems,_size*,data,len*);
}
vector& vector::operator =(const vector&a){
if(*this==a) return *this;
delete[] this->elems;
this->_size=a._size;
this->_capacity=a._capacity;
this->elems=new int[_capacity];
memcpy_s(this->elems,_capacity*,a.elems,_capacity*);
return *this;
}
int vector::size()const{
return this->_size;
}
int vector::capacity()const{
return this->_capacity;
}
void vector::reserve(int capacity_){
if(capacity_<=this->_capacity)return;
else{
this->_capacity=capacity_;
int *es=new int[_capacity];
memset(es,0x3f,_capacity*);
memcpy_s(es,_size*,elems,_size*);
delete[] elems;
this->elems=es;
}
}
void vector::resize(int size_){
if(size_<this->_size)this->_size=size_;
else if(size_<=this->_capacity){
while(this->_size<size_){
this->elems[this->_size++]=-;
}
}else if(size_>this->_capacity){
this->reserve(size_);
while(this->_size<this->_capacity){
this->elems[this->_size++]=-;
}
}
} int& vector::operator [](int p){
if(_safePos(p))
return this->elems[p];
exit();
}
void vector::insert(int pos, int x){
if(!_safePos(pos))exit();
if(this->_size==this->_capacity){
reserve(this->_capacity+this->_size/);
}
for(int j=this->_size;j>pos;j--){
this->elems[j]=this->elems[j-];
}
this->elems[pos-]=x;
}
void vector::erase(int pos){
if(!this->_safePos(pos))exit();
for(int i=pos;i<this->_size-;i++){
this->elems[i]=this->elems[i+];
}
this->_size-=;
}
void vector::push_back(int x){
if(this->_capacity==this->_size){
this->reserve(this->_capacity+this->_size/);
this->elems[this->_size++]=x;
}else{
this->elems[this->_size++]=x;
}
}
void vector::pop_back(){
if(!this->empty())
--this->_size;
}
int& vector::front(){
return const_cast<int&>(static_cast<const vector&>(*this).front());//把指针转成const型,调用下面一个。再将结果的转成非const
}
const int& vector::front() const{
return this->elems[];
}
int& vector::back(){
return const_cast<int&>(static_cast<const vector*>(this)->back());//这里试试两种不同的写法而已
}
const int& vector::back() const{
return this->elems[_size-];
}
bool vector::empty()const{
return _size==?true:false;
}

简单的 vector的更多相关文章

  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. C++线性序列容器<vector>简单总结

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

  9. 线性表实现简单vector

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

随机推荐

  1. SpringBoot2.x入门:应用打包与启动

    前提 这篇文章是<SpringBoot2.x入门>专辑的第5篇文章,使用的SpringBoot版本为2.3.1.RELEASE,JDK版本为1.8. 这篇文章分析一个偏向于运维方面的内容: ...

  2. 用python实现栈/队列/双端队列/链表

    栈是元素的有序集合,添加操作与移除操作都发生在其顶端,先进后出栈操作:创建空栈,增删(顶端),查(顶端元素,元素个数,是否为空)应用:将十进制数转换成任意进制数 class Stack: # 用列表创 ...

  3. 02 drf源码剖析之快速了解drf

    02 drf源码剖析之快速了解drf 目录 02 drf源码剖析之快速了解drf 1. 什么是drf 2. 安装 3. 使用 3. DRF的应用场景 1. 什么是drf drf是一个基于django开 ...

  4. The solution for apt-get update Err 404

    最近在ubuntu 12.10上执行sudo apt-get update 命令后出现了如下错误: Ign http://extras.ubuntu.com natty/main Translatio ...

  5. MySql-Binlog协议

    MySQL主备复制原理 MySQL master 将数据变更写入二进制日志( binary log, 其中记录叫做二进制日志事件binary log events,可以通过 show binlog e ...

  6. 事件的event对象基本解释

    事件流: 描述的是在页面中接受事件的顺序主要分为两种: 事件冒泡.事件捕获 事件event对象:1. type 获取事件类型2. target获取事件目标3. stopPropagation() 阻止 ...

  7. [spring cloud] -- 服务注册与服务发现篇

    eureka 服务发现客户端 DiscoveryClinet职责(核心) 注册服务无试了到Eureka Server中; 发送新塘更新与Eureka Server的租约: 在服务关闭时从Eureka ...

  8. Python 正则表达式简单了解

    match 从字符串的开始匹配  如果开头不符合要求  就会报错 search  用字符串里的每一个元素  去匹配找的元素 1.匹配单个字符 \d 数字 \D 非数字 . 匹配任意字符 除了\n [] ...

  9. Object#wait()与Object#wait(long)的区别

    例子 例1 最基础的等待-通知 下面一个例子,一个线程"waiting"在同步代码块调用了Object#wait()方法,另一个线程"timedWaiting" ...

  10. 解释Crypto模块,No module named "Crypto"

    1.pip install pycryptodome 2.Python\Python38\Lib\site-packages,找到这个路径,下面有一个文件夹叫做crypto,将小写c改成大写C就ok了 ...