#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. appium升级操作

    在app自动化测试中经常会碰到,因为appium版本低而导致,appium客户端连接不到appium服务端等一系列错误~ 其实appium升级很简单的哦~ 打开cmd命令行终端,键入npm updat ...

  2. Java入门系列之访问修饰符作用范围

    前言 之前以为Java和C#中访问修饰符差不多一样,后面才了解到还是有些差异,本节只讲解学习Java中访问修饰符一些需要注意的地方或者从概念上不太好理解我们会通过实际例子来说明,若有错误之处,还请批评 ...

  3. Linux多任务编程之六:编写多进程程序及其代码(转)

    来源:CSDN  作者:王文松  转自Linux公社 ------------------------------------------------------------------------- ...

  4. day10 字符编码

    字符编码 在python中出现乱码就是字符编码没有匹配的问题 python3中执行python3编辑的代码只要没有修改过编码,都是用utf-8,如果出现乱码就修改头文件,改成和原来编码相同的字符编码 ...

  5. 大话一个CPU(沙子是如何影响未来的)

    大话一个CPU(沙子是如何影响未来的) CPU是个啥? 先大体上了解一下 中央处理器 (英语:Central Processing Unit,缩写:CPU),是计算机的主要设备之一,功能主要是解释计算 ...

  6. scrapy 基础组件专题(七):scrapy 调度器、调度器中间件、自定义调度器

    一.调度器 配置 SCHEDULER = 'scrapy.core.scheduler.Scheduler' #表示scrapy包下core文件夹scheduler文件Scheduler类# 可以通过 ...

  7. bzoj3687简单题*

    bzoj3687简单题 题意: 给个集合,求所有子集的元素和的异或和.集合元素个数≤1000,整个集合的元素和≤2000000 题解: 用bitset维护每个子集元素和的个数是奇数还是偶数.每次读入一 ...

  8. java 正则提取字符串中的电话号码

    public static void test2() { String str = "张三:13539558064,李四:15626829748,赵六:13718952204"; ...

  9. linux管理防火墙

    操作系统环境:CentOS Linux release 7.0.1406(Core) 64位CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤. 1.关闭f ...

  10. GPO - File Server Management

    Creating disk space usage quotas: File Screening Generate Storage Report, including file edit audit. ...