143.vector模板库

  • myvector.h

     1 #pragma once
     2 #include <initializer_list>
     3 #include <iostream>
     4 using namespace std;
     5
     6 template<class T>
     7 class myvector
     8 {
     9 public:
    10     myvector();
    11     myvector(int n);
    12     myvector(initializer_list<T> my);
    13     void show() const;
    14     ~myvector();
    15     //用于迭代
    16     T *begin();
    17     T *end();
    18     int arraysize();
    19     int memsize();
    20     //后插
    21     void push_back(T data);
    22     //前插
    23     void push_front(T data);
    24     //返回的是副本
    25     T operator[](int i) const;
    26     //返回的不是副本
    27     T &operator[](int i);
    28     //发现左值
    29     T* find(T &t);
    30     //发现右值
    31     T* find(T &&t);
    32     //改变一个值,传递的是右值
    33     void change(T *pos, T &&t);
    34     //改变一个值,传递的是左值
    35     void change(T *pos, T &t);
    36     void del(T &t);
    37     void del(T &&t);
    38     //找到的位置之前插入
    39     void insert(T &t, T &newt);
    40     void insert(T &&t, T &&newt);
    41
    42     template<class T>
    43     friend ostream& operator <<(ostream &out, myvector<T> &myv);
    44 public:
    45     T * p;
    46     int memn;//内存长度
    47     int realn;//数组长度
    48 };
  • myvector.cpp

      1 #include "myvector.h"
      2
      3 template<class T>
      4 myvector<T>::myvector():p(nullptr),memn(0),realn(0)
      5 {
      6
      7 }
      8
      9 template<class T>
     10 myvector<T>::myvector(int n)
     11 {
     12     this->memn = this->realn = n;
     13     this->p = new T[n];
     14     memset(this->p, 0, sizeof(T)*n);
     15 }
     16
     17 template<class T>
     18 myvector<T>::myvector(initializer_list<T> my)
     19 {
     20     this->realn = this->memn = my.size();
     21     this->p = new T[my.size()];
     22     int id = 0;
     23     //初始化
     24     for (auto i : my)
     25     {
     26         this->p[id] = i;
     27         id++;
     28     }
     29 }
     30
     31 template<class T>
     32 void myvector<T>::show() const
     33 {
     34     for (int i = 0; i < realn; i++)
     35     {
     36         cout << p[i];
     37     }
     38 }
     39
     40 template<class T>
     41 myvector<T>::~myvector()
     42 {
     43     if (p != nullptr)
     44     {
     45         delete[] this->p;
     46     }
     47 }
     48
     49 template<class T>
     50 T * myvector<T>::begin()
     51 {
     52     return this->p;
     53 }
     54
     55 template<class T>
     56 T * myvector<T>::end()
     57 {
     58     return this->p + this->realn;
     59 }
     60
     61 template<class T>
     62 int myvector<T>::arraysize()
     63 {
     64     return this->realn;
     65 }
     66
     67 template<class T>
     68 int myvector<T>::memsize()
     69 {
     70     return this->memn;
     71 }
     72
     73 template<class T>
     74 void myvector<T>::push_back(T data)
     75 {
     76     if (this->p == nullptr || this->memn == 0)
     77     {
     78             p = new T;
     79             *p = data;
     80             memn = 1;
     81             realn = 1;
     82     }
     83     else if(memn == realn)
     84     {
     85         T *ptemp = new T[this->memn + 1];
     86         memcpy(ptemp, this->p, this->realn * sizeof(T));
     87         *(ptemp + realn) = data;
     88         delete[] this->p;
     89         this->p = ptemp;
     90         realn += 1;
     91         memn += 1;
     92     }
     93     else
     94     {
     95         p[realn] = data;
     96         realn++;
     97     }
     98 }
     99
    100 template<class T>
    101 void myvector<T>::push_front(T  data)
    102 {
    103     if (this->p == nullptr || this->memn == 0)
    104     {
    105         p = new T;
    106         *p = data;
    107         memn = 1;
    108         realn = 1;
    109     }
    110     else if (memn == realn)
    111     {
    112         T *ptemp = new T[this->memn + 1];
    113         memcpy(ptemp + 1, this->p, this->realn * sizeof(T));
    114         *ptemp = data;
    115         delete[] this->p;
    116         this->p = ptemp;
    117         realn += 1;
    118         memn += 1;
    119     }
    120     else
    121     {
    122         for (int i = 0; i < realn; i++)
    123         {
    124             p[i + 1] = p[i];
    125         }
    126         p[0] = data;
    127         realn += 1;
    128     }
    129 }
    130
    131 template<class T>
    132 T myvector<T>::operator[](int i) const
    133 {
    134     if (i > realn)
    135     {
    136         throw 1;
    137     }
    138     return this->p[i];
    139 }
    140
    141 template<class T>
    142 T & myvector<T>::operator[](int i)
    143 {
    144     if (i > realn)
    145     {
    146         throw 2;
    147     }
    148     return this->p[i];
    149 }
    150
    151 template<class T>
    152 T * myvector<T>::find(T & t)
    153 {
    154     for (auto ib = this->begin(); ib != this->end(); ib++)
    155     {
    156         if (*it == t)
    157         {
    158             return ib;
    159         }
    160     }
    161 }
    162
    163 template<class T>
    164 T * myvector<T>::find(T && t)
    165 {
    166     for (auto ib = this->begin(); ib != this->end(); ib++)
    167     {
    168         if (*it == t)
    169         {
    170             return ib;
    171         }
    172     }
    173 }
    174
    175 template<class T>
    176 void myvector<T>::change(T * pos, T && t)
    177 {
    178     if (pos != nullptr)
    179     {
    180         *pos = t;
    181     }
    182 }
    183
    184 template<class T>
    185 void myvector<T>::change(T * pos, T & t)
    186 {
    187     if (pos != nullptr)
    188     {
    189         *pos = t;
    190     }
    191 }
    192
    193 template<class T>
    194 void myvector<T>::del(T & t)
    195 {
    196     int pos = -1;
    197     for (int i = 0; i < this->realn; i++)
    198     {
    199         if (t == *(this->p + i))
    200         {
    201             pos = i;
    202             break;
    203         }
    204     }
    205     if (pos != -1)
    206     {
    207         if (pos == this->realn)
    208         {
    209             this->realn -= 1;
    210         }
    211         else
    212         {
    213             for (int i = pos; i < realn-1; i++)
    214             {
    215                 p[i] = p[i + 1];
    216             }
    217             realn -= 1;
    218         }
    219     }
    220 }
    221
    222 template<class T>
    223 void myvector<T>::del(T && t)
    224 {
    225     int pos = -1;
    226     for (int i = 0; i < this->realn; i++)
    227     {
    228         if (t == *(this->p + i))
    229         {
    230             pos = i;
    231             break;
    232         }
    233     }
    234     if (pos != -1)
    235     {
    236         if (pos == this->realn)
    237         {
    238             this->realn -= 1;
    239         }
    240         else
    241         {
    242             for (int i = pos; i < realn - 1; i++)
    243             {
    244                 p[i] = p[i + 1];
    245             }
    246             realn -= 1;
    247         }
    248     }
    249 }
    250
    251
    252 template<class T>
    253 void myvector<T>::insert(T & t, T & newt)
    254 {
    255     int pos = -1;
    256     for (int i = 0; i < this->realn; i++)
    257     {
    258         if (t == *(this->p + i))
    259         {
    260             pos = i;
    261             break;
    262         }
    263     }
    264
    265     if (pos != -1)
    266     {
    267         if (this->realn == this->memn)
    268         {
    269                 T *ptemp = new T[this->memn + 1];
    270                 memcpy(ptemp, this->p, sizeof(T)*memn);
    271
    272                 delete[] this->p;
    273                 this->p = ptemp;
    274                 this->realn += 1;
    275                 this->memn += 1;
    276                 for (int i = realn - 2; i >= pos; i--)
    277                 {
    278                     p[i + 1] = p[i];
    279                 }
    280                 p[pos] = newt;
    281         }
    282         else
    283         {
    284             for (int i = realn - 2; i >= pos; i--)
    285             {
    286                 p[i + 1] = p[i];
    287             }
    288             p[pos] = newt;
    289             this->realn += 1;
    290         }
    291     }
    292
    293 }
    294
    295 template<class T>
    296 void myvector<T>::insert(T && t, T && newt)
    297 {
    298     int pos = -1;
    299     for (int i = 0; i < this->realn; i++)
    300     {
    301         if (t == *(this->p + i))
    302         {
    303             pos = i;
    304             break;
    305         }
    306     }
    307
    308     if (pos != -1)
    309     {
    310         if (this->realn == this->memn)
    311         {
    312             T *ptemp = new T[this->memn + 1];
    313             memcpy(ptemp, this->p, sizeof(T)*memn);
    314
    315             delete[] this->p;
    316             this->p = ptemp;
    317             this->realn += 1;
    318             this->memn += 1;
    319             for (int i = realn - 2; i >= pos; i--)
    320             {
    321                 p[i + 1] = p[i];
    322             }
    323             p[pos] = newt;
    324         }
    325         else
    326         {
    327             for (int i = realn - 2; i >= pos; i--)
    328             {
    329                 p[i + 1] = p[i];
    330             }
    331             p[pos] = newt;
    332             this->realn += 1;
    333         }
    334     }
    335 }
    336
    337 template<class T>
    338 ostream & operator<<(ostream & out, myvector<T> & myv)
    339 {
    340     for (int i = 0; i < myv.realn; i++)
    341     {
    342         out << myv.p[i];
    343     }
    344     return out;
    345 }
  • main.cpp

     1 #include "myvector.h"
     2 #include "myvector.cpp"
     3
     4 void main()
     5 {
     6     myvector<double> myv({ 1.1,2.2,3.3 });
     7     //myv.show();
     8     ////迭代器
     9     //for (auto i : myv)
    10     //{
    11     //    cout << i << endl;
    12     //}
    13     myv.push_front(4.4);
    14     myv.push_back(5.5);
    15     myv.insert(2.2, 8.8);
    16     myv.del(2.2);
    17     myv.push_back(2.2);
    18     myv.del(2.2);
    19     /*for (auto ib = myv.begin(), ie = myv.end(); ib != ie; ib++)
    20     {
    21         cout << *ib << endl;
    22     }*/
    23     //cout << myv[1] << endl;
    24     cout << myv << endl;
    25     myv.show();
    26     cin.get();
    27 }

原文地址:https://www.cnblogs.com/xiaochi/p/8711425.html

时间: 2024-08-29 09:58:33

143.vector模板库的相关文章

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

标准模板库(STL)学习探究之vector容器  C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,能够增加和压缩数据.为了可以使用vector,必须在你的头文件中包含下面的代码:#include <vector>构造函数. Vectors 包含着一系列连续存储的元素,其行为和数组类

C++标准模板库之vector(Boolan)

vector是C++标准模板库中一种常见的容器,像数组类似,vector使用连续的存储空间用来保存元素,使用指针偏移可以快速的访问元素(通常认为是O1复杂度),与数组不同的是它的大小是可变的,在vector内部使用动态分配的内存保存元素,这意味着vector增长时需要重新分配内存,并将原来的原来的数据复制到该内存单元,需要很大的开销,因此vector并不会在每次新增元素时候都重新分配内存空间.vector实际容量(capacity)通常比实际大小(size)大得多.参考数据结构(C++语言版),

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

摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解.即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第一篇,主要针对线性表中的顺序表(动态数组)STL vector进行分析和总结. 引言 因为前段时间对台大的机器学习基石和技法课程进行了学习,发如今详细的实现中经常涉及到各种类型的数据结构,比方线性表.二叉树.图等,在使用这些数据结构时感到有些吃力.主要是对一些主要的数据结构理解的不够.所以趁着暑假假期.近期一段时间总会抽出时间复习一

C++ Primer(第五版)学习笔记_3_标准模板库vector(2)

C++ Primer(第五版)学习笔记_3_标准模板库vector(2) 欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢 6.元素的插入 insert()方法可以在vector对象的任意位置前插入一个新的元素,同时,vector自动扩张一个元素空间,插入位置后的所有元素依次向后挪动一个位置. 要注意的是,insert()方法要求插入的位置,是元素的迭代器位置,而不是元素的下标. #include <iostream> #include <vector> using namespa

C++ Primer(第五版)学习笔记_2_标准模板库vector(1)

C++ Primer(第五版)学习笔记_2_标准模板库vector(1) 欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢 向量容器vector不但能像数组一样进行随机访问,还能在尾部插入元素,完全可以替代数组. 值得注意的是,vector具有内存自动管理的功能,对于元素的插入和删除,可以动态调整所占的内存空间. 容器vector的下标是从0开始的,如果vector容器的大小是n,则元素下标为0~n-1,这和数组的一样的.不一样的是,vector可以随时调整其大小. vector重要的方法有三个

C++STL模板库之vector

目录 STL之Vecter 一丶STL简介 二丶Vector用法 1.vector容器的使用 2.vector迭代器. 3.vector中的方法. STL之Vecter 一丶STL简介 STL 是标准模板库的意思. 就是数据结构,封装成类让我们使用. 使用的时候我们要了解数据结构才可以使用这些类.因为数据结构不知道是什么结构你用类的话也用不明白. 二丶Vector用法 1.vector容器的使用 首先介绍的第一个序列容器就是 vector. 它底层是数组.可以理解为是动态数组. 我们自己也可以写

标准模板库

-------------------siwuxie095 在长期的编码中,聪明的程序员们发现:有一些代码经常碰到, 而且需求特别稳定,于是,各大公司在出售自己的 IDE 环境时, 就会把这些模板代码打包,一起销售 慢慢地,这些大公司之间就达成了某种共识,觉得应该把这些 涉及模板的通用代码进一步的统一和规范,于是,大家慢慢形 成了一套 C++ 的标准模板,就是现在所看到的标准模板库 标准模板库 标准模板库,即 Standard Template Lib,简称为 STL 标准模板库所涉及的内容非常

C++模板引出的标准模板库-----&gt;初涉

C++中模板,是相当重要的一部分,前面提到过一些基础,关于模板中需要注意的问题,会在最近整理出来,今天想说的,是由模板引出的标准模板库. 当初经常会被推荐看<STL源码剖析>这本书,听说很厉害,是C++高手都需要走过的路,可一直都不知道STL是什么,也一直忘记去查,今天整理出来的一些东西,最起码可以让未了解过这方面的童鞋认识一下. C++标准模板库,即STL:Standard Template Lib,STL的产生,是必然的.在长期的编码过程中,一些程序员发现,有一些代码经常用到,而且需求特别

C++标准库和标准模板库

C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义. 在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括: (1)成本:已经作为标准提供,何苦再花费时间.人力重新开发呢: (2)质量:标准库的都是经过严格测试的,正确性有保证: (3)效率:关于人的效率已经体现在成本中了,关于代码的执行效率要相信实现标准库的大牛们的水平: (4)良好的编程风格:采用行业中普遍的做法进行开发. 一.C++标准库 C++标准库的内容分为10类, 分别是:C1.语