• myvector.h

     #pragma once
    #include <initializer_list>
    #include <iostream>
    using namespace std; template<class T>
    class myvector
    {
    public:
    myvector();
    myvector(int n);
    myvector(initializer_list<T> my);
    void show() const;
    ~myvector();
    //用于迭代
    T *begin();
    T *end();
    int arraysize();
    int memsize();
    //后插
    void push_back(T data);
    //前插
    void push_front(T data);
    //返回的是副本
    T operator[](int i) const;
    //返回的不是副本
    T &operator[](int i);
    //发现左值
    T* find(T &t);
    //发现右值
    T* find(T &&t);
    //改变一个值,传递的是右值
    void change(T *pos, T &&t);
    //改变一个值,传递的是左值
    void change(T *pos, T &t);
    void del(T &t);
    void del(T &&t);
    //找到的位置之前插入
    void insert(T &t, T &newt);
    void insert(T &&t, T &&newt); template<class T>
    friend ostream& operator <<(ostream &out, myvector<T> &myv);
    public:
    T * p;
    int memn;//内存长度
    int realn;//数组长度
    };
  • myvector.cpp
     #include "myvector.h"
    
     template<class T>
    myvector<T>::myvector():p(nullptr),memn(),realn()
    { } template<class T>
    myvector<T>::myvector(int n)
    {
    this->memn = this->realn = n;
    this->p = new T[n];
    memset(this->p, , sizeof(T)*n);
    } template<class T>
    myvector<T>::myvector(initializer_list<T> my)
    {
    this->realn = this->memn = my.size();
    this->p = new T[my.size()];
    int id = ;
    //初始化
    for (auto i : my)
    {
    this->p[id] = i;
    id++;
    }
    } template<class T>
    void myvector<T>::show() const
    {
    for (int i = ; i < realn; i++)
    {
    cout << p[i];
    }
    } template<class T>
    myvector<T>::~myvector()
    {
    if (p != nullptr)
    {
    delete[] this->p;
    }
    } template<class T>
    T * myvector<T>::begin()
    {
    return this->p;
    } template<class T>
    T * myvector<T>::end()
    {
    return this->p + this->realn;
    } template<class T>
    int myvector<T>::arraysize()
    {
    return this->realn;
    } template<class T>
    int myvector<T>::memsize()
    {
    return this->memn;
    } template<class T>
    void myvector<T>::push_back(T data)
    {
    if (this->p == nullptr || this->memn == )
    {
    p = new T;
    *p = data;
    memn = ;
    realn = ;
    }
    else if(memn == realn)
    {
    T *ptemp = new T[this->memn + ];
    memcpy(ptemp, this->p, this->realn * sizeof(T));
    *(ptemp + realn) = data;
    delete[] this->p;
    this->p = ptemp;
    realn += ;
    memn += ;
    }
    else
    {
    p[realn] = data;
    realn++;
    }
    } template<class T>
    void myvector<T>::push_front(T data)
    {
    if (this->p == nullptr || this->memn == )
    {
    p = new T;
    *p = data;
    memn = ;
    realn = ;
    }
    else if (memn == realn)
    {
    T *ptemp = new T[this->memn + ];
    memcpy(ptemp + , this->p, this->realn * sizeof(T));
    *ptemp = data;
    delete[] this->p;
    this->p = ptemp;
    realn += ;
    memn += ;
    }
    else
    {
    for (int i = ; i < realn; i++)
    {
    p[i + ] = p[i];
    }
    p[] = data;
    realn += ;
    }
    } template<class T>
    T myvector<T>::operator[](int i) const
    {
    if (i > realn)
    {
    throw ;
    }
    return this->p[i];
    } template<class T>
    T & myvector<T>::operator[](int i)
    {
    if (i > realn)
    {
    throw ;
    }
    return this->p[i];
    } template<class T>
    T * myvector<T>::find(T & t)
    {
    for (auto ib = this->begin(); ib != this->end(); ib++)
    {
    if (*it == t)
    {
    return ib;
    }
    }
    } template<class T>
    T * myvector<T>::find(T && t)
    {
    for (auto ib = this->begin(); ib != this->end(); ib++)
    {
    if (*it == t)
    {
    return ib;
    }
    }
    } template<class T>
    void myvector<T>::change(T * pos, T && t)
    {
    if (pos != nullptr)
    {
    *pos = t;
    }
    } template<class T>
    void myvector<T>::change(T * pos, T & t)
    {
    if (pos != nullptr)
    {
    *pos = t;
    }
    } template<class T>
    void myvector<T>::del(T & t)
    {
    int pos = -;
    for (int i = ; i < this->realn; i++)
    {
    if (t == *(this->p + i))
    {
    pos = i;
    break;
    }
    }
    if (pos != -)
    {
    if (pos == this->realn)
    {
    this->realn -= ;
    }
    else
    {
    for (int i = pos; i < realn-; i++)
    {
    p[i] = p[i + ];
    }
    realn -= ;
    }
    }
    } template<class T>
    void myvector<T>::del(T && t)
    {
    int pos = -;
    for (int i = ; i < this->realn; i++)
    {
    if (t == *(this->p + i))
    {
    pos = i;
    break;
    }
    }
    if (pos != -)
    {
    if (pos == this->realn)
    {
    this->realn -= ;
    }
    else
    {
    for (int i = pos; i < realn - ; i++)
    {
    p[i] = p[i + ];
    }
    realn -= ;
    }
    }
    } template<class T>
    void myvector<T>::insert(T & t, T & newt)
    {
    int pos = -;
    for (int i = ; i < this->realn; i++)
    {
    if (t == *(this->p + i))
    {
    pos = i;
    break;
    }
    } if (pos != -)
    {
    if (this->realn == this->memn)
    {
    T *ptemp = new T[this->memn + ];
    memcpy(ptemp, this->p, sizeof(T)*memn); delete[] this->p;
    this->p = ptemp;
    this->realn += ;
    this->memn += ;
    for (int i = realn - ; i >= pos; i--)
    {
    p[i + ] = p[i];
    }
    p[pos] = newt;
    }
    else
    {
    for (int i = realn - ; i >= pos; i--)
    {
    p[i + ] = p[i];
    }
    p[pos] = newt;
    this->realn += ;
    }
    } } template<class T>
    void myvector<T>::insert(T && t, T && newt)
    {
    int pos = -;
    for (int i = ; i < this->realn; i++)
    {
    if (t == *(this->p + i))
    {
    pos = i;
    break;
    }
    } if (pos != -)
    {
    if (this->realn == this->memn)
    {
    T *ptemp = new T[this->memn + ];
    memcpy(ptemp, this->p, sizeof(T)*memn); delete[] this->p;
    this->p = ptemp;
    this->realn += ;
    this->memn += ;
    for (int i = realn - ; i >= pos; i--)
    {
    p[i + ] = p[i];
    }
    p[pos] = newt;
    }
    else
    {
    for (int i = realn - ; i >= pos; i--)
    {
    p[i + ] = p[i];
    }
    p[pos] = newt;
    this->realn += ;
    }
    }
    } template<class T>
    ostream & operator<<(ostream & out, myvector<T> & myv)
    {
    for (int i = ; i < myv.realn; i++)
    {
    out << myv.p[i];
    }
    return out;
    }
  • main.cpp
     #include "myvector.h"
    #include "myvector.cpp" void main()
    {
    myvector<double> myv({ 1.1,2.2,3.3 });
    //myv.show();
    ////迭代器
    //for (auto i : myv)
    //{
    // cout << i << endl;
    //}
    myv.push_front(4.4);
    myv.push_back(5.5);
    myv.insert(2.2, 8.8);
    myv.del(2.2);
    myv.push_back(2.2);
    myv.del(2.2);
    /*for (auto ib = myv.begin(), ie = myv.end(); ib != ie; ib++)
    {
    cout << *ib << endl;
    }*/
    //cout << myv[1] << endl;
    cout << myv << endl;
    myv.show();
    cin.get();
    }

143.vector模板库的更多相关文章

  1. 35.自己实现vector模板库myvector

    myvector.h #pragma once //自己写的vector模板库 template <class T> class myvector { public: //构造 myvec ...

  2. c++转载系列 std::vector模板库用法介绍

    来源:http://blog.csdn.net/phoebin/article/details/3864590 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作 ...

  3. C++ 标准模板库(STL)——容器(Containers)的用法及理解

    C++ 标准模板库(STL)中定义了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量(vector).队列(queue).栈(stack).set.map等.这次主要 ...

  4. 标准模板库(STL)学习探究之vector容器

    标准模板库(STL)学习探究之vector容器  C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被 ...

  5. C++STL模板库序列容器之vector

    目录 STL之Vecter 一丶STL简介 二丶Vector用法 1.vector容器的使用 2.vector迭代器. 3.vector中的方法. 三丶常用算法 1.常见算法中的算法方法. 2.sor ...

  6. C++标准模板库(STL)之Vector

    在C中,有很多东西需要自己实现.C++提供了标准模板库(Standard Template Libray,STL),其中封装了很多容器,不需要费力去实现它们的细节而直接调用函数来实现功能. 具体容器链 ...

  7. C++的标准模板库STL中实现的数据结构之顺序表vector的分析与使用

    摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解.即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第一篇,主要针对线性表中的顺序表(动 ...

  8. C++:标准模板库vector

    一:介绍 vector是C++标准模板库,是一个容器,底层是数组,为连续内存. 命名空间为std,所属头文件为<vector>   注意:不是<vector.h> vector ...

  9. STL标准模板库(简介)

    标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...

随机推荐

  1. 杂项-DB:DW/DWH(数据仓库)

    ylbtech-杂项-DB:DW/DWH(数据仓库) 数据仓库,英文名称为Data Warehouse,可简写为DW或DWH.数据仓库,是为企业所有级别的决策制定过程,提供所有类型数据支持的战略集合. ...

  2. 如何用php实现qq登陆网站

    PHP网站入QQ互联,使用QQ号码登录网站. 平台接口系列文章 PHP网站入QQ互联,使用QQ号码登录网站 PHP网站接入人人网,授权登陆 php facebook api网站接入facebook 1 ...

  3. .NET平台开源JSON序列化

    转载: http://blog.csdn.net/ddgweb/article/details/39643747 一个简单示例: String str = "{’name’:’cyf’,’i ...

  4. Tomcat 初探(三)多项目部署

    起因 昨天房东找我说最近物价飞涨,要涨我房租,混不下去了,得要求老板涨工资.一大清早就去找老板,老板看了看我,让我发布先两个网站:一个前台网站 frontend,给用户用:一个后台管理网站 backe ...

  5. DIV水平方向居中的几种方法

    一.使用margin: 1 #center0 { 2 background: red; 3 margin: 0 auto; 4 } 或者: margin: auto; 这样的前提是父盒子里没有其他盒子 ...

  6. Android跳转到系统Wifi界面的方式

    第一种 Intent intent = new Intent(); intent.setAction("android.net.wifi.PICK_WIFI_NETWORK"); ...

  7. intell-

    intellect: n.[U, C] the ability to think in a logical way and understand things, especially at an ad ...

  8. PhotoZoom正式版和试用版的区别是什么?

    通常的工具对数码图片进行放大时,总会降低图片的品质,而这款软体使用了S-SPLINE技术(一种申请过专利的,拥有自动调节.进阶的插值算法的技术),可以将尽可能地提高放大图片的品质.程序最大的特色是可以 ...

  9. 深度学习之入门Pytorch(1)------基础

    目录: Pytorch数据类型:Tensor与Storage 创建张量 tensor与numpy数组之间的转换 索引.连接.切片等 Tensor操作[add,数学运算,转置等] GPU加速 自动求导: ...

  10. kvm三个kernel相关的调优

    今天在杭州参加淘宝嘉年华技术沙龙,主题是虚拟化和云计算,三个讲演: 淘宝网子团分享淘宝kvm技术的使用 华为的杨晓伟介绍虚拟化技术 阿里云郑永升介绍弹性云计算技术 其中KVM 调优,三点值得关注的: ...