• C++实现简易Vector类


    用C++实现了一下简易的Vector类,主要是回忆起来练习下C++的模板类以及模板函数的使用。

    参数博文:https://blog.csdn.net/happy_lucky52/article/details/76167125

    主要实现了Vector的各个构造函数,Vector的常见操作。

    博主水平堪忧,代码可能有Bug。

    Vector.h

      1 #pragma once
      2 #include<iostream>
      3 #include<cassert>
      4 #include<algorithm>
      5 using namespace std;
      6 
      7 const int DEAULT_CAPACITY = 5;
      8 
      9 template<typename T>
     10 class Vector {
     11 public:
     12     //各种构造函数和析构函数 
     13     Vector(int cap = DEAULT_CAPACITY, int sz = 0, const T v = T()) :
     14         capacity(cap), size(sz) {
     15         data = new T[capacity];
     16         for (int i = 0; i < size; i++) data[i] = v;
     17     }
     18     Vector(const T *arr, int n) {
     19         size = n;
     20         capacity = n;
     21         data = new T[capacity];
     22         for (int i = 0; i < size; i++) data[i] = arr[i];
     23     }
     24     Vector(const T *arr, int l, int r);
     25     Vector(const Vector<T> &v) {
     26         size = v.size;
     27         capacity = v.capacity;
     28         data = new T[capacity];
     29         for (int i = 0; i < size; i++) data[i] = v.data[i];
     30     }
     31     Vector(const Vector<T> &v, int l, int r) {
     32         size = r - l;
     33         capacity = size;
     34         data = new T[capacity];
     35         for (int i = 0; i < size; i++) data[i] = v.data[i + l];
     36     }
     37     ~Vector() { delete[] data; }
     38     Vector<T>& operator = (const Vector<T> &v);
     39 
     40     //获取信息 
     41     int GetCapacity() const { return capacity; }
     42     int GetSize() const { return size; }
     43     bool empty() const { return (size == 0); }
     44     int find(const T &v) const;
     45 
     46     //改变元素 
     47     void insert(int p, const T &v);
     48     void remove(int p);
     49     void CopyFrom(const T *v, int l, int r);
     50     void push_back(const T &v);
     51     void pop_back();
     52     void clear();
     53     void sort();
     54 
     55     //重载运算符
     56     /*T& operator [] (const int p) {
     57         assert(p >= 0 && p < size);
     58         return data[p];
     59     }*/
     60     template<typename T>
     61     friend ostream& operator << (ostream &o, const Vector<T> &v);
     62 
     63 private:
     64     void expand();
     65     static void swap(T &a, T &b) { T tmp = a; a = b; b = tmp; }
     66 
     67 protected:
     68     int capacity;    //容量 
     69     int size;        //Vector大小 
     70     T *data;        //实际数据 
     71 };
     72 
     73 template<typename T>
     74 ostream& operator << (ostream &o, const Vector<T> &v) {
     75     for (int i = 0; i < v.GetSize(); i++)
     76         o << v.data[i] << " ";
     77     return o;
     78 }
     79 
     80 template<typename T>
     81 Vector<T>& Vector<T>::operator= (const Vector<T> &v) {
     82     if (data == v.data) return *this;
     83     delete[]data;
     84     size = v.size;
     85     capacity = v.capacity;
     86     data = new T[v.capacity];
     87     CopyFrom(v.data, 0, size);
     88 }
     89 
     90 template<typename T>
     91 void Vector<T>::CopyFrom(const T *v, int l, int r) {
     92     size = 0;
     93     for (int i = l; i < r; i++)
     94         data[size++] = v[i];
     95 }
     96 
     97 template<typename T>
     98 void Vector<T>::expand() {
     99     if (size == capacity) {
    100         T *OldData = data;
    101         if (capacity < DEAULT_CAPACITY) capacity = DEAULT_CAPACITY;        //防止capacity为0时候出错
    102         capacity <<= 1;
    103         data = new T[capacity];
    104         CopyFrom(OldData, 0, size);
    105         delete[] OldData;
    106     }
    107 }
    108 
    109 template<typename T>
    110 void Vector<T>::insert(int p, const T& v) {
    111     assert(p >= 0 && p <= size);
    112     expand();
    113     for (int i = size - 1; i >= p; i--) data[i + 1] = data[i];
    114     data[p] = v;
    115     size++;
    116 }
    117 
    118 template<typename T>
    119 void Vector<T>::push_back(const T &v) {
    120     insert(size, v);
    121 }
    122 
    123 template<typename T>
    124 void Vector<T>::remove(int p) {
    125     for (int i = p; i < size-1; i++)
    126         data[i] = data[i + 1];
    127     size--;
    128 }
    129 
    130 template<typename T>
    131 void Vector<T>::pop_back() {
    132     assert(size > 0);
    133     remove(size - 1);
    134 }
    135 
    136 template<typename T>
    137 void Vector<T>::clear() {
    138     while (size) pop_back();
    139 }
    140 
    141 template<typename T>
    142 int Vector<T>::find(const T &p) const {
    143     for (int i = 0; i < size; i++)
    144         if (p == data[i]) return i;
    145     return -1;
    146 }
    147 
    148 template<typename T>
    149 void Vector<T>::sort() {
    150     std::sort(data, data + size);
    151 }

    Test.cpp

     1 #include "pch.h"
     2 #include"Vector.h"
     3 #include <iostream>
     4 using namespace std;
     5 
     6 //测试各种Vector操作
     7 void Test1() {
     8     Vector<int> v1;
     9     v1.push_back(1);
    10     v1.push_back(2);
    11     v1.push_back(3);
    12     v1.push_back(4);
    13     cout << v1 << endl;
    14     v1.pop_back();
    15     cout << v1.GetSize() << endl;
    16     v1.push_back(1);
    17     cout << v1 << endl;
    18     cout << v1.find(1) << endl;
    19     v1.remove(v1.find(1));
    20     cout << v1 << endl;
    21     v1.push_back(55); v1.push_back(66);
    22     cout << v1.GetCapacity() << " " << v1.GetSize() << " " << v1 << endl;
    23     v1.push_back(77);
    24     cout << v1.GetCapacity() << " " << v1.GetSize() << " " << v1 << endl;
    25     v1.clear();
    26     cout << v1.GetSize()<<" "<<v1 << endl;
    27 }
    28 //测试不同的构造函数
    29 void Test2() {
    30     Vector<int> v2(5, 5, 2);
    31     cout << v2.GetCapacity() << " " << v2.GetSize() << " " << v2 << endl;
    32     Vector<int> v3(v2);
    33     v3.push_back(61);
    34     cout << v3.GetCapacity() << " " << v3.GetSize() << " " << v3 << endl;
    35     v2 = v3;
    36     cout << v2.GetCapacity() << " " << v2.GetSize() << " " << v2 << endl;
    37 
    38     int a[]{ 9,1,5,3,4 };
    39     Vector<int> v4(a, 5);
    40     cout << v4.GetCapacity() << " " << v4.GetSize() << " " << v4 << endl;
    41 
    42     v4.sort();
    43     cout << "排序后:" << endl;
    44     cout << v4 << endl;
    45 
    46     char b[]{ '9','1','5','3','4' };
    47     Vector<char> v5(b, 5);
    48     cout << v5.GetCapacity() << " " << v5.GetSize() << " " << v5 << endl;
    49 }
    50 
    51 int main()
    52 {
    53     //Test1();
    54     Test2();
    55     return 0;
    56 }
     
  • 相关阅读:
    C# 创建一个日志文件
    C# WinForm程序添加引用后调用静态方法时报“Interfaces_Helper.Global”的类型初始值设定项引发异常。---> System.NullReferenceException: 未将对象引用设置到对象的实例。
    SqlServer 一些操作
    多线程处理sql server2008出现Transaction (Process ID) was deadlocked on lock resources with another process and has been chose问题
    SQL
    WinForm 弹框确认后执行
    C#强制清除缓存
    C#TextBox自动滚动到最低端
    XmlDocument To String
    ROCK 算法
  • 原文地址:https://www.cnblogs.com/clno1/p/12825703.html
Copyright © 2020-2023  润新知