• 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. 点击了一个link button,查看后台调用

    使用F12进行监视 本身是一个linkbutton,可以看到绑定了一个JavaScript <a id="gvStaticConnection_ctl02_fresh" hr ...

  2. Java IO 基础

    早上复习了IO.NIO.AIO相关的概念,将其中一些要点记录一下. 从编程语言层面 BIO | NIO | AIO 以Java的角度,理解,linux c里也有AIO的概念(库),这些概念不知道什么原 ...

  3. javascript系列-class12.事件

    1.默认行为          什么是默认行为:默认行为就是浏览器自己触发的事件.比如:a链接的跳转,form提交时的跳转,鼠标右键跳转:   oncontexmenu当点击右键菜单的时候:   re ...

  4. 2017.7.15清北夏令营精英班Day1解题报告

    成绩: 预计分数:20+10+40 实际分数:100+10+40. 一百三十多人的比赛全场rand7还水了个鼠标+键盘 unbelievable! 考试题目链接: https://www.luogu. ...

  5. 用Latex做介绍自己和团队科研的网页

    最近实验室师妹用网上的一些模板改了改做了几个网页.感觉还可以.但是实际上总觉得好像和韩家炜.周志华他们的页面差点什么. 最近找论文时发现奥地利的hornik老先生页面居然latex做的,然后找到了下面 ...

  6. ssh: connect to host port 22: Connection refused

    配置Linux的互信,互信完成后,连接需要+端口才可以​​已经将端口修改为7777,但依旧走的是默认的22端口​​修改方式:​[root@yoon ssh]# vi /etc/servicesssh ...

  7. js禁止

    很多时候需要用到js禁止相关的代码: function prohibit() { // 禁止右键 $(document).ready(function() { $(document).bind(&qu ...

  8. UVa 424 Integer Inquiry 【大数相加】

    解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当 ...

  9. Photoshop把图片调成固定的像素。

    1.用PhotoShop打开需要修改的图片. 2.点击“窗口”菜单的“图层”子菜单,打开图层控制面板(快捷键F7).3.用鼠标左键双击“图层”面板的“背景”图层.在弹出窗口中点击“确定”按钮,解锁背景 ...

  10. Arduino扫盲(持续添加中)

    1.Arduino火的很,很大一点在于,他基本透明掉了硬件电子部分,只剩下软件部分,通过把电子部分包装成黑箱,使得大量IT人士,普通人,甚至小学生也能玩的来. 2 .Arduino是一个电子原型开发平 ...